Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // To compile: gcc -std=gnu99 -O2 -o performance performance.c
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <time.h>
- #define arrcpy(dest,src,size) for(size_t zz=0;zz<size;zz++)dest[zz]=src[zz]
- #define dalloc(size) (test_t*)malloc(sizeof(test_t)*size)
- typedef double test_t;
- const size_t SIZE = 1E4;
- const size_t ITERATIONS = 1E6;
- const int MATH_TEST = 2;
- const int MEMORY_TEST = 1;
- inline void ptrcpy( test_t *src, test_t *dest, size_t size ) {
- test_t *srcptr = src+size, *destptr = dest+size;
- while ( srcptr > src ) {
- *(--destptr) = *(--srcptr);
- }
- }
- inline void ptrcpy2( test_t *src, test_t *dest, size_t size ) {
- test_t *srcptr = src, *destptr = dest;
- test_t *srcend = src+size;
- while ( srcptr < srcend ) {
- *(destptr++) = *(srcptr++);
- }
- }
- int main( int argc, char **argv ) {
- size_t i, j;
- test_t *src = dalloc( SIZE );
- test_t *dest = dalloc( SIZE );
- test_t *dest2 = dalloc( SIZE );
- time_t start_time, run_time;
- int tests = MATH_TEST | MEMORY_TEST;
- if ( argc > 1 && !strcmp( argv[1], "math" )) {
- tests = MATH_TEST;
- }
- if ( argc > 1 && !strcmp( argv[1], "mem" )) {
- tests = MEMORY_TEST;
- }
- if ( tests & MEMORY_TEST ) {
- printf( "Test data type size: %d bits\n", sizeof( test_t ) << 3 );
- printf( "Number of elements: %d\n", SIZE );
- printf( "Number of iterations: %d\n", ITERATIONS );
- printf( "Running memory read/write tests...\n" );
- time( &start_time );
- for( i=0; i < ITERATIONS; i++ ) {
- memcpy( dest, src, sizeof( test_t ) * SIZE );
- memcpy( dest2, src, sizeof( test_t ) * SIZE );
- }
- run_time = time( NULL ) - start_time;
- printf( "memcpy() run time %d seconds.\n", (int)run_time );
- time( &start_time );
- for( i=0; i < ITERATIONS; i++ ) {
- for( j=0; j < SIZE; j++ ) {
- dest[ j ] = src[ j ];
- dest2[ j ] = src[ j ];
- }
- }
- run_time = time( NULL ) - start_time;
- printf( "for() run time %d seconds.\n", (int)run_time );
- time( &start_time );
- for( i=0; i < ITERATIONS; i++ ) {
- for( j=0; j < SIZE; j++ ) {
- dest[ j ] = src[ j ];
- }
- for( j=0; j < SIZE; j++ ) {
- dest2[ j ] = src[ j ];
- }
- }
- run_time = time( NULL ) - start_time;
- printf( "dual for() run time %d seconds.\n", (int)run_time );
- // time( &start_time );
- // for( i=0; i < ITERATIONS; i++ ) {
- // arrcpy( dest, src, SIZE );
- // arrcpy( dest2, src, SIZE );
- // }
- // run_time = time( NULL ) - start_time;
- // printf( "arrcpy() run time %d seconds.\n", (int)run_time );
- time( &start_time );
- for( i=0; i < ITERATIONS; i++ ) {
- ptrcpy( dest, src, SIZE );
- ptrcpy( dest2, src, SIZE );
- }
- run_time = time( NULL ) - start_time;
- printf( "ptrcpy() run time %d seconds.\n", (int)run_time );
- time( &start_time );
- for( i=0; i < ITERATIONS; i++ ) {
- ptrcpy2( dest, src, SIZE );
- ptrcpy2( dest2, src, SIZE );
- }
- run_time = time( NULL ) - start_time;
- printf( "ptrcpy2() run time %d seconds.\n", (int)run_time );
- }
- free( src );
- free( dest );
- free( dest2 );
- // I'm not sure these are really giving an accurate picture. May need more work.
- if ( tests & MATH_TEST ) {
- printf( "Running math tests...\n" );
- time( &start_time );
- for( i=0; i < ITERATIONS * SIZE / 10; i++ ) {
- drand48( ) * drand48( );
- }
- run_time = time( NULL ) - start_time;
- printf( "Floating point multiplication test run time %d seconds.\n", (int)run_time );
- time( &start_time );
- for( i=0; i < ITERATIONS * SIZE / 10; i++ ) {
- rand( ) * rand( );
- }
- run_time = time( NULL ) - start_time;
- printf( "Integer multiplication test run time %d seconds.\n", (int)run_time );
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment