Skip to content

Instantly share code, notes, and snippets.

@jesuslpm
Created October 3, 2013 11:16
Show Gist options
  • Select an option

  • Save jesuslpm/6808179 to your computer and use it in GitHub Desktop.

Select an option

Save jesuslpm/6808179 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
unsigned char RandomByte(unsigned char maxValue)
{
return (unsigned char) ((unsigned long)rand() * maxValue / RAND_MAX);
}
void FillByteArrayWithUniqueValues(unsigned char *byteArray)
{
unsigned char remainingValues[256];
int i, lastIndex = 255;
for (i = 0; i < 256; i++)
{
remainingValues[i] = i;
}
while (lastIndex != 0)
{
unsigned char b = RandomByte(lastIndex);
byteArray[lastIndex] = remainingValues[b];
for(i = b; i < lastIndex; i++)
{
remainingValues[i] = remainingValues[i+1];
}
lastIndex--;
}
}
int main()
{
unsigned char byteArray[4][256];
int *uniqueNumbers;
clock_t start, end;
int i, uniqueNumbersToGenerate = 1000 * 1000, *np, byteIndex;
double elapsed;
start = clock();
for (i= 0; i < 4; i++)
{
FillByteArrayWithUniqueValues(byteArray[i]);
}
uniqueNumbers = (int*) malloc(sizeof(int) * uniqueNumbersToGenerate);
np = uniqueNumbers;
for (i = 0; i < uniqueNumbersToGenerate; i++, np++)
{
for (byteIndex = 0; byteIndex < 4; byteIndex++)
{
((unsigned char*)np)[byteIndex] = byteArray[byteIndex][(i >> (8 * byteIndex)) & 0x000000FF];
}
}
end = clock();
elapsed = (double) (end - start) / CLOCKS_PER_SEC;
printf("Elapsed seconds %lf \n", elapsed);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment