Last active
August 29, 2015 14:02
-
-
Save cypres/8d54a87904b94259ce67 to your computer and use it in GitHub Desktop.
Bench of integer concats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "time.h" | |
#include "math.h" | |
#include "assert.h" | |
#include <string> | |
// After http://ideone.com/hbWgE | |
int num_tests = 5000000; | |
unsigned duck(unsigned x, unsigned y) { | |
unsigned pow = 10; | |
while(y >= pow) | |
pow *= 10; | |
return x * pow + y; | |
} | |
int myPow(int x, int p) | |
{ | |
if (p == 0) return 1; | |
if (p == 1) return x; | |
int tmp = myPow(x, p/2); | |
if (p%2 == 0) return tmp * tmp; | |
else return x * tmp * tmp; | |
} | |
int david(int x, int y) { | |
int power = log10(y); | |
return x*myPow(10,power+1)+y; | |
} | |
int drummer(int x, int y) { | |
int digits = log10(y)+1; | |
int shifted = x * pow(10, digits); // will be 1100 in your example | |
return shifted + y; // 1111 | |
} | |
int shabeer(int x, int y) { | |
int temp=0; | |
int z=x; | |
while(y>0) | |
{ | |
// take reciprocal of y into temp | |
temp=(temp*10)+(y%10); | |
y=y/10; | |
} | |
while(temp>0) | |
{ | |
// take each number from last of temp and add to last of z | |
z=(z*10)+(temp%10); | |
temp=temp/10; | |
} | |
return z; | |
} | |
int gokcehan(int x, int y) { | |
int temp = y; | |
while (y != 0) { | |
x *= 10; | |
y /= 10; | |
} | |
return x + temp; | |
} | |
int naive(int x, int y) { | |
return std::stol(std::to_string(x) + std::to_string(y)); | |
} | |
int stupid(int x, int y) { | |
char buf[16]; | |
snprintf(buf, sizeof(buf), "%i%i", x, y); | |
return std::stol(buf); | |
} | |
void int_time(const char* name, int(*func)(int, int)) { | |
clock_t start = clock(); | |
int r = 0; | |
int i = 0; | |
for(; i<num_tests; ++i) | |
r += func(i, i); | |
printf("%s found %u in %d\n", name, r, clock()-start); | |
} | |
void uint_time(const char* name, unsigned(*func)(unsigned, unsigned)) { | |
clock_t start = clock(); | |
unsigned r = 0; | |
unsigned i = 0; | |
for(; i<num_tests; ++i) | |
r += func(i, i); | |
printf("%s found %u in %d\n", name, r, clock()-start); | |
} | |
void uint_accuracy(const char* name, unsigned(*func)(unsigned, unsigned)) { | |
printf("testing %s accuracy: ", name); | |
assert(func(0, 0) == 0); | |
assert(func(0, 1) == 1); | |
assert(func(0, 10) == 10); | |
assert(func(1, 0) == 10); | |
assert(func(1, 1) == 11); | |
assert(func(1, 10) == 110); | |
assert(func(10, 0) == 100); | |
assert(func(10, 1) == 101); | |
assert(func(10, 10) == 1010); | |
assert(func(23, 456) == 23456); | |
assert(func(234, 56) == 23456); | |
printf("pass\n"); | |
} | |
void int_accuracy(const char* name, int(*func)(int, int)) { | |
printf("testing %s accuracy: ", name); | |
assert(func(0, 0) == 0); | |
assert(func(0, 1) == 1); | |
assert(func(0, 10) == 10); | |
assert(func(1, 0) == 10); //several fail this one | |
assert(func(1, 1) == 11); | |
assert(func(1, 10) == 110); | |
assert(func(10, 0) == 100); //several fail this one | |
assert(func(10, 1) == 101); | |
assert(func(10, 10) == 1010); | |
assert(func(23, 456) == 23456); | |
assert(func(234, 56) == 23456); | |
printf("pass\n"); | |
} | |
int main() { | |
uint_time("duck", duck); | |
uint_time("duck", duck); | |
int_time("david", david); | |
int_time("david", david); | |
int_time("drummer", drummer); | |
int_time("drummer", drummer); | |
int_time("shabeer", shabeer); | |
int_time("shabeer", shabeer); | |
int_time("gokcehan", gokcehan); | |
int_time("gokcehan", gokcehan); | |
int_time("stupid", stupid); | |
int_time("stupid", stupid); | |
int_time("naive", stupid); | |
int_time("naive", stupid); | |
uint_accuracy("duck", duck); | |
//int_accuracy("gokcehan", gokcehan); //Assertion `func(1, 0) == 10' failed. | |
//int_accuracy("drummer", drummer); //Assertion `func(1, 0) == 10' failed. | |
//int_accuracy("david", david); //Assertion `func(1, 0) == 10' failed. | |
//int_accuracy("shabeer", shabeer); //Assertion `func(0, 10) == 10' failed. | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
duck found 2343525334 in 54586 | |
duck found 2343525334 in 54567 | |
david found 2343525334 in 230050 | |
david found 2343525334 in 229112 | |
drummer found 3188974518 in 306875 | |
drummer found 3188974518 in 306296 | |
shabeer found 2299299461 in 790372 | |
shabeer found 2299299461 in 786956 | |
gokcehan found 2343525334 in 212261 | |
gokcehan found 2343525334 in 217507 | |
stupid found 2343525334 in 1543151 | |
stupid found 2343525334 in 1504988 | |
naive found 2343525334 in 1497291 | |
naive found 2343525334 in 1513641 | |
testing duck accuracy: pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment