add speed comparison
git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@375 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
parent
7eb9f2e6ae
commit
6b5d0e2629
1 changed files with 125 additions and 112 deletions
|
@ -14,6 +14,8 @@
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
#include "../SmallObj/timer.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
template <class Integral1, class Integral2>
|
template <class Integral1, class Integral2>
|
||||||
|
@ -58,7 +60,7 @@ void TestCase(const string& fmt, T value)
|
||||||
char buf[4096];
|
char buf[4096];
|
||||||
std::string s;
|
std::string s;
|
||||||
const int i1 = SPrintf(s, fmt.c_str())(value);
|
const int i1 = SPrintf(s, fmt.c_str())(value);
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
const int i2 = _snprintf(buf, sizeof(buf), fmt.c_str(), value);
|
const int i2 = _snprintf(buf, sizeof(buf), fmt.c_str(), value);
|
||||||
#else
|
#else
|
||||||
|
@ -90,124 +92,135 @@ void TestCase2(const string& fmt, T value, U value2)
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
if (argc == 3)
|
if (argc == 2)
|
||||||
{
|
{
|
||||||
// test speed
|
// test speed
|
||||||
int i = atoi(argv[2]);
|
|
||||||
switch (argv[1][0])
|
Timer t;
|
||||||
{
|
|
||||||
case 'p':
|
int loop = atoi(argv[1]);
|
||||||
for (; i > 0; --i)
|
|
||||||
{
|
if(loop < 100)
|
||||||
printf("Hey, %u frobnicators and %u twiddlicators\n",
|
loop = 100;
|
||||||
i, i);
|
|
||||||
}
|
t.start();
|
||||||
break;
|
for (int i=loop; i > 0; --i)
|
||||||
case 's':
|
printf("Hey, %u frobnicators and %u twiddlicators\n",i, i);
|
||||||
for (; i > 0; --i)
|
t.stop();
|
||||||
{
|
t.t100 = t.t();
|
||||||
cout << "Hey, " << i << " frobnicators and " << i <<
|
int t_printf = t.t();
|
||||||
" twiddlicators\n";
|
|
||||||
}
|
|
||||||
break;
|
t.start();
|
||||||
case 'n':
|
for (int i=loop; i > 0; --i)
|
||||||
for (; i > 0; --i)
|
Printf("Hey, %u frobnicators and %u twiddlicators\n")(i)(i);
|
||||||
{
|
t.stop();
|
||||||
Printf("Hey, %u frobnicators and %u twiddlicators\n")
|
int t_Printf = t.t();
|
||||||
(i)(i);
|
|
||||||
}
|
|
||||||
break;
|
t.start();
|
||||||
}
|
for (int i=loop; i > 0; --i)
|
||||||
return 0;
|
cout << "Hey, " << i << " frobnicators and " << i <<" twiddlicators\n";
|
||||||
|
t.stop();
|
||||||
|
int t_cout = t.t();
|
||||||
|
|
||||||
|
|
||||||
|
Printf("\n\nElapsed time for %i outputs\n\n")(loop);
|
||||||
|
t.print(t_printf,"printf : ");
|
||||||
|
t.print(t_Printf,"Printf : ");
|
||||||
|
t.print(t_cout, "std::cout: ");
|
||||||
}
|
}
|
||||||
|
else
|
||||||
//srand(time(0));
|
|
||||||
srand(0);
|
|
||||||
for (unsigned i = 0; ; ++i)
|
|
||||||
{
|
{
|
||||||
printf("%u\r", i);
|
//srand(time(0));
|
||||||
|
srand(0);
|
||||||
|
printf("\nNumber of tests:\n");
|
||||||
|
for (unsigned i = 0; ; ++i)
|
||||||
|
{
|
||||||
|
printf("%u\r", i);
|
||||||
|
|
||||||
// Generate a random string for the head
|
// Generate a random string for the head
|
||||||
string lead = RandomString(100);
|
string lead = RandomString(100);
|
||||||
// This string will hold a random format specification
|
// This string will hold a random format specification
|
||||||
string formatSpec(lead + "|%");
|
string formatSpec(lead + "|%");
|
||||||
// Generate a random set of flags
|
// Generate a random set of flags
|
||||||
static const string flags("-+0 #");
|
static const string flags("-+0 #");
|
||||||
unsigned int maxFlags = RandomInt(0u, flags.length() - 1);
|
unsigned int maxFlags = RandomInt(0u, flags.length() - 1);
|
||||||
for (unsigned int i = 0; i != maxFlags; ++i)
|
for (unsigned int i = 0; i != maxFlags; ++i)
|
||||||
{
|
|
||||||
formatSpec += flags[RandomInt(0u, flags.length() - 1)];
|
|
||||||
}
|
|
||||||
// Generate an optional random width
|
|
||||||
if (RandomInt(0, 1))
|
|
||||||
{
|
|
||||||
const unsigned int width = RandomInt(0, 100);
|
|
||||||
char buf[4];
|
|
||||||
sprintf(buf, "%u", width);
|
|
||||||
formatSpec += buf;
|
|
||||||
}
|
|
||||||
// Generate an optional random precision
|
|
||||||
if (RandomInt(0, 1))
|
|
||||||
{
|
|
||||||
const unsigned int prec = RandomInt(0, 100);
|
|
||||||
char buf[4];
|
|
||||||
sprintf(buf, "%u", prec);
|
|
||||||
formatSpec += '.';
|
|
||||||
formatSpec += buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate a random type character
|
|
||||||
/* disable %p tests
|
|
||||||
static const string type("cdeEfgGinopsuxX");*/
|
|
||||||
static const string type("cdeEfgGinosuxX");
|
|
||||||
|
|
||||||
const char typeSpec = type[RandomInt(0, type.size() - 1)];
|
|
||||||
// Generate an optional type prefix
|
|
||||||
static const string prefix("hl");
|
|
||||||
if (typeSpec != 's' && RandomInt(0, 1))
|
|
||||||
{
|
|
||||||
formatSpec += prefix[RandomInt(0, prefix.size() - 1)];
|
|
||||||
}
|
|
||||||
formatSpec += typeSpec;
|
|
||||||
formatSpec += '|';
|
|
||||||
formatSpec += RandomString(100);
|
|
||||||
|
|
||||||
switch (typeSpec)
|
|
||||||
{
|
|
||||||
case 'c':
|
|
||||||
TestCase(formatSpec, RandomInt(1, 127));
|
|
||||||
break;
|
|
||||||
case 'd':
|
|
||||||
case 'i':
|
|
||||||
case 'o':
|
|
||||||
case 'u':
|
|
||||||
case 'x':
|
|
||||||
case 'X':
|
|
||||||
TestCase(formatSpec, RandomInt(-10000, 10000));
|
|
||||||
break;
|
|
||||||
case 'e':
|
|
||||||
case 'E':
|
|
||||||
case 'f':
|
|
||||||
case 'g':
|
|
||||||
case 'G':
|
|
||||||
TestCase(formatSpec,
|
|
||||||
RandomInt(-10000, 10000) / double(RandomInt(1, 100)));
|
|
||||||
break;
|
|
||||||
case 'n':
|
|
||||||
break;
|
|
||||||
case 'p':
|
|
||||||
{
|
{
|
||||||
void * p = malloc(RandomInt(1, 1000));
|
formatSpec += flags[RandomInt(0u, flags.length() - 1)];
|
||||||
TestCase(formatSpec, p);
|
}
|
||||||
free(p);
|
// Generate an optional random width
|
||||||
|
if (RandomInt(0, 1))
|
||||||
|
{
|
||||||
|
const unsigned int width = RandomInt(0, 100);
|
||||||
|
char buf[4];
|
||||||
|
sprintf(buf, "%u", width);
|
||||||
|
formatSpec += buf;
|
||||||
|
}
|
||||||
|
// Generate an optional random precision
|
||||||
|
if (RandomInt(0, 1))
|
||||||
|
{
|
||||||
|
const unsigned int prec = RandomInt(0, 100);
|
||||||
|
char buf[4];
|
||||||
|
sprintf(buf, "%u", prec);
|
||||||
|
formatSpec += '.';
|
||||||
|
formatSpec += buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate a random type character
|
||||||
|
/* disable %p tests
|
||||||
|
static const string type("cdeEfgGinopsuxX");*/
|
||||||
|
static const string type("cdeEfgGinosuxX");
|
||||||
|
|
||||||
|
const char typeSpec = type[RandomInt(0, type.size() - 1)];
|
||||||
|
// Generate an optional type prefix
|
||||||
|
static const string prefix("hl");
|
||||||
|
if (typeSpec != 's' && RandomInt(0, 1))
|
||||||
|
{
|
||||||
|
formatSpec += prefix[RandomInt(0, prefix.size() - 1)];
|
||||||
|
}
|
||||||
|
formatSpec += typeSpec;
|
||||||
|
formatSpec += '|';
|
||||||
|
formatSpec += RandomString(100);
|
||||||
|
|
||||||
|
switch (typeSpec)
|
||||||
|
{
|
||||||
|
case 'c':
|
||||||
|
TestCase(formatSpec, RandomInt(1, 127));
|
||||||
|
break;
|
||||||
|
case 'd':
|
||||||
|
case 'i':
|
||||||
|
case 'o':
|
||||||
|
case 'u':
|
||||||
|
case 'x':
|
||||||
|
case 'X':
|
||||||
|
TestCase(formatSpec, RandomInt(-10000, 10000));
|
||||||
|
break;
|
||||||
|
case 'e':
|
||||||
|
case 'E':
|
||||||
|
case 'f':
|
||||||
|
case 'g':
|
||||||
|
case 'G':
|
||||||
|
TestCase(formatSpec,
|
||||||
|
RandomInt(-10000, 10000) / double(RandomInt(1, 100)));
|
||||||
|
break;
|
||||||
|
case 'n':
|
||||||
|
break;
|
||||||
|
case 'p':
|
||||||
|
{
|
||||||
|
void * p = malloc(RandomInt(1, 1000));
|
||||||
|
TestCase(formatSpec, p);
|
||||||
|
free(p);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 's':
|
||||||
|
TestCase(formatSpec, RandomString(100).c_str());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
assert(false);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
case 's':
|
|
||||||
TestCase(formatSpec, RandomString(100).c_str());
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
assert(false);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue