Skip to content

Instantly share code, notes, and snippets.

@nitko12
Created June 16, 2022 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nitko12/845b6abeb15c40528748c70670ed9ff9 to your computer and use it in GitHub Desktop.
Save nitko12/845b6abeb15c40528748c70670ed9ff9 to your computer and use it in GitHub Desktop.
Testing itoa speeds of semi naive unrolled approach againt jeaiii black magic code
#include <random>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "jeaiii_to_text.h.h"
using namespace std;
using namespace jeaiii;
void itoa_test(unsigned int n, char *s)
{
// 1 to 8 digits
if (n < 10000)
{
if (n < 100)
{
if (n < 10)
{
// 1
s[0] = '0' + n;
s[1] = 0;
}
else
{
// 2
s[0] = '0' + n / 10;
s[1] = '0' + n % 10;
s[2] = 0;
}
}
else
{
if (n < 1000)
{
// 3
s[0] = '0' + n / 100;
s[1] = '0' + n / 10 % 10;
s[2] = '0' + n % 10;
s[3] = 0;
}
else
{
// 4
s[0] = '0' + n / 1000;
s[1] = '0' + n / 100 % 10;
s[2] = '0' + n / 10 % 10;
s[3] = '0' + n % 10;
s[4] = 0;
}
}
}
else
{
if (n < 1000000)
{
if (n < 100000)
{
// 5
s[0] = '0' + n / 10000;
s[1] = '0' + n / 1000 % 10;
s[2] = '0' + n / 100 % 10;
s[3] = '0' + n / 10 % 10;
s[4] = '0' + n % 10;
s[5] = 0;
}
else
{
// 6
s[0] = '0' + n / 100000;
s[1] = '0' + n / 10000 % 10;
s[2] = '0' + n / 1000 % 10;
s[3] = '0' + n / 100 % 10;
s[4] = '0' + n / 10 % 10;
s[5] = '0' + n % 10;
s[6] = 0;
}
}
else
{
if (n < 10000000)
{
// 7
s[0] = '0' + n / 1000000;
s[1] = '0' + n / 100000 % 10;
s[2] = '0' + n / 10000 % 10;
s[3] = '0' + n / 1000 % 10;
s[4] = '0' + n / 100 % 10;
s[5] = '0' + n / 10 % 10;
s[6] = '0' + n % 10;
s[7] = 0;
}
else
{
// 8
s[0] = '0' + n / 10000000;
s[1] = '0' + n / 1000000 % 10;
s[2] = '0' + n / 100000 % 10;
s[3] = '0' + n / 10000 % 10;
s[4] = '0' + n / 1000 % 10;
s[5] = '0' + n / 100 % 10;
s[6] = '0' + n / 10 % 10;
s[7] = '0' + n % 10;
s[8] = 0;
}
}
}
}
int n = 1000000;
int a[1000005];
const int range_from = 0;
const int range_to = 10000;
int main()
{
random_device rand_dev;
mt19937 generator(rand_dev());
uniform_int_distribution<int> distr(range_from, range_to);
for (int i = 0; i < n; ++i)
{
a[i] = distr(generator);
}
char t[100];
for (int i : {0, 1, 12, 123, 1234, 12345, 123456, 1234567, 12345678})
{
itoa_test(i, t);
printf("%d %s\n", i, t);
}
for (int i = 0; i < 5; ++i)
{
itoa_test(a[i], t);
printf("%d %s\n", a[i], t);
}
clock_t begin, end;
double difference;
// begin = clock();
// for (int i = 0; i < n; ++i)
// {
// itoa(a[i], t, 100);
// }
// end = clock();
// difference = (double)(end - begin) / CLOCKS_PER_SEC;
// printf("itoa: %lf\n", difference);
begin = clock();
for (int i = 0; i < n; ++i)
{
to_text_from_integer(t, a[i]);
}
end = clock();
difference = (double)(end - begin) / CLOCKS_PER_SEC;
printf("jeaiii: %lf\n", difference);
begin = clock();
for (int i = 0; i < n; ++i)
{
itoa_test(a[i], t);
}
end = clock();
difference = (double)(end - begin) / CLOCKS_PER_SEC;
printf("myitoa: %lf\n", difference);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment