Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****
- *
- * modified version from Preston L. Bannister
- *
- **/
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- #include <string.h>
- /*******************************************************************************
- *******************************************************************************
- * configs
- *******************************************************************************
- ******************************************************************************/
- #define LOOPS 4000000
- /*******************************************************************************
- *******************************************************************************
- * engine
- *******************************************************************************
- ******************************************************************************/
- unsigned int dtLoop = 0;
- unsigned int nTotal = 0;
- typedef void (*doit)();
- void report_times(const char* s, unsigned int dt, int bytes, int scale)
- {
- double ts = (double)dt / CLOCKS_PER_SEC;
- double mb = (double)(bytes)*LOOPS*scale / 1024/1024 ;
- double rate = mb / ts;
- printf("%s:\t\t %0.4f seconds (%0.1f MB in %u clocks)\n", s, ts, mb, dt);
- }
- int time_function(doit fn, int bytes)
- {
- clock_t t0 = clock();
- int i=0;
- for (i; i<LOOPS*bytes; ++i) {
- (*fn)();
- }
- return (int)(clock() - t0);
- }
- /*******************************************************************************
- * end engine
- ******************************************************************************/
- /*******************************************************************************
- *******************************************************************************
- * benchmark
- *******************************************************************************
- ******************************************************************************/
- void do_2_bytes()
- {
- malloc(2);
- }
- void do_4_bytes()
- {
- malloc(4);
- }
- void do_16_bytes()
- {
- malloc(16);
- }
- void do_32_bytes()
- {
- malloc(32);
- }
- void do_128_bytes()
- {
- malloc(128);
- }
- void do_256_bytes()
- {
- malloc(256);
- }
- /*******************************************************************************
- * end benchmark
- ******************************************************************************/
- /*******************************************************************************
- *******************************************************************************
- * main programm
- *******************************************************************************
- ******************************************************************************/
- int main(int ac,char** av)
- {
- int i = atoi(av[1]);
- if(i==2)
- report_times("alloc 2 bytes", time_function(do_2_bytes, 20), 2, 20);
- if(i==4)
- report_times("alloc 4 bytes", time_function(do_4_bytes, 10), 4, 10);
- if(i==16)
- report_times("alloc 16 bytes", time_function(do_16_bytes, 2), 16, 2);
- if(i==32)
- report_times("alloc 32 bytes", time_function(do_32_bytes, 1), 32, 1);
- if(i==128)
- report_times("alloc 128 bytes", time_function(do_128_bytes, 1), 128, 1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement