Created
May 30, 2014 15:27
-
-
Save radbasa/e8844923063191ea0d4a to your computer and use it in GitHub Desktop.
TCC on DOSBox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.c | |
// optimize | |
// | |
// Created by Rodrigo Basa on 2014-05-29. | |
// Copyright (c) 2014 Rodrigo Basa. All rights reserved. | |
// | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
int main(int argc, const char * argv[]) { | |
const int samples = 30000; | |
clock_t lessifs = 0; | |
clock_t orig = 0; | |
clock_t moreifs = 0; | |
clock_t start, end; | |
int i, w, x, f; | |
char z; | |
srand( (unsigned) time( NULL ) ); | |
for ( i = 0; i < samples; i++ ) { | |
// Common set of data | |
w = rand() % 2; // 0 or 1 | |
x = rand() % 80; // 0 to 80 | |
f = rand() % 2; // 0 or 1 converted to 'F' and 'M' below | |
if ( f == 1 ) | |
z = 'M'; | |
else | |
z = 'F'; | |
//Original | |
start = clock(); | |
if ( w == 1 ) { | |
if ( z == 'M' ) { | |
if ( x >= 20 && x <= 65 ) { | |
// statements 1 | |
} else if ( x < 20 ) { | |
// statements 3 | |
} | |
} else { | |
if ( x >= 20 ) { | |
if ( x <= 65 ) { | |
// statements 1 | |
// statements 2 | |
} else { | |
// statements 2 | |
} | |
} | |
} | |
} else { | |
if ( z == 'M' ) { | |
if ( x < 20 ) { | |
// statements 3 | |
} | |
} else { | |
if ( x >= 20 ) { | |
// statements 2 | |
} | |
} | |
} | |
end = clock(); | |
orig += ( end - start ); | |
// Less ifs, no nesting | |
// measure only the time spent in the conditional blocks | |
start = clock(); | |
if ( (w == 1) && (x >= 20) && (x <= 65) ) { | |
// no statements to isolate execution time of the conditional and comparisons | |
} | |
if ( (x >= 20) && (z == 'F') ) { | |
// | |
} | |
if ( (x < 20) && (z == 'M') ) { | |
// | |
} | |
end = clock(); | |
lessifs += ( end - start ); | |
// More ifs, with nesting | |
// measure only the time spent in the conditional blocks | |
start = clock(); | |
if ( x < 20 ) { | |
if ( z == 'M' ) { | |
// | |
} | |
} else { | |
if ( x <= 65 && w == 1 ) { | |
// | |
} | |
if ( z == 'F' ) { | |
// | |
} | |
} | |
end = clock(); | |
moreifs += ( end - start ); | |
} | |
printf( "Original : %f \n", orig/CLK_TCK ); | |
printf( "Less ifs, no nesting : %f \n" , lessifs/CLK_TCK ); | |
printf( "More ifs, with nesting : %f \n" , moreifs/CLK_TCK ); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment