Skip to content

Instantly share code, notes, and snippets.

@eienf
Last active December 10, 2015 22:08
Show Gist options
  • Save eienf/4500125 to your computer and use it in GitHub Desktop.
Save eienf/4500125 to your computer and use it in GitHub Desktop.
time measurement for rand/random/arc4random.
//
// main.m
// CodeTest
//
// Created by eien.support@gmail.com on 2013/01/11.
// Copyright (c) 2013 Eien Factory. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <mach/mach_time.h>
#import <stdlib.h>
#import <ScreenSaver/ScreenSaver.h>
#define TEST 4
int main(int argc, const char * argv[])
{
@autoreleasepool {
uint64_t start, elapsed;
mach_timebase_info_data_t base;
mach_timebase_info(&base);
unsigned int seed;
seed = (unsigned int)time(NULL);
srandom(seed);
srand(seed);
start = mach_absolute_time();
for (int i=0; i<1000*1000; i++) {
long value;
#if TEST==1
value = arc4random();//time = 12 939 828(nsec)
#elif TEST==2
value = random();//time = 6 012 601(nsec)
#elif TEST==3
value = rand();//time = 6 720 593(nsec)
#elif TEST==4
value = SSRandomIntBetween(0,100);//time = 9 549 701(nsec)
#endif
}
elapsed = mach_absolute_time() - start;
uint64_t nsec = elapsed * base.numer / base.denom;
printf("time = %lld(nsec)\n",nsec);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment