//===================================================== file = alloc4.c ===== //= Times indexing into one-time allocated memory = //= - Sequential and random reads to study caching effects = //=========================================================================== //= Notes: = //= 1) Must manually set #define for WIN or UNIX compile = //= 2) Must manually set #define NUM = //= 3) Note that an index value is computed within the sequential loop = //= so that overhead is same as in random loop. The non-use of index = //= in the first loop results in a compiler warning. Ignore it. = //= 4) NUM memory locations are malloc'ed at the start. = //=-------------------------------------------------------------------------= //= Example execution: = //= = //= ----------------------------------------------- alloc3.c ----- = //= It took 1.362 sec to do 10000000 sequential indexes into memory = //= It took 1.933 sec to do 10000000 random indexes into memory = //= --------------------------------------------------------------- = //=-------------------------------------------------------------------------= //= Build: bcc32 alloc3.c, gcc alloc3.c = //=-------------------------------------------------------------------------= //= Execute: alloc3 = //=-------------------------------------------------------------------------= //= Author: Kenneth J. Christensen = //= University of South Florida = //= WWW: http://www.csee.usf.edu/~christen = //= Email: christen@csee.usf.edu = //=-------------------------------------------------------------------------= //= History: KJC (12/17/00) - Genesis (from timeit.c) = //=========================================================================== #define WIN // WIN for Windows and UNIX for Unix //----- Include files ------------------------------------------------------- #include // Needed for printf() #include // Needed for malloc(), rand(), and exit() #ifdef WIN #include // Needed for ftime() and timeb structure #endif #ifdef UNIX #include // Needed for ftime() and timeb structure #endif //----- Defines ------------------------------------------------------------- #define NUM 10000000 // Number of allocations to perform //=========================================================================== //= Main program = //=========================================================================== void main(void) { unsigned int *val; // Unsigned int value unsigned int temp; // Temporary unsigned int value struct timeb start, stop; // Start and stop times structures double elapsed1; // Elapsed time in seconds for sequential double elapsed2; // Elapsed time in seconds for random unsigned int i; // Loop counter unsigned int index; // Array index // Output a banner printf("----------------------------------------------- alloc4.c -----\n"); // Do the malloc all at once (outside of the timing loop) and assign values val = malloc(NUM * sizeof(unsigned int)); if (val == NULL) { printf("ERROR - malloc failed with NULL pointer (NUM = %d) \n", NUM); exit(1); } for (i=0; i