Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- fibonacci C benchmark program v1.3
- made by James Wait. forgive my terrible coding, it's messy and inefficient as this is one of my first C programs
- Compile with "gcc -o benchmark benchmark.c"
- calculates 40th fibonacci number 8 times, records each calculation time and multiplies it by 8.
- It then averages these numbers and returns the result as the final score.
- There are no command line arguments.... yet
- Also... don't use this as a serious benchmarking tool. it's inacurate and bad in many other ways
- but it gives you a rough indication of how fast your computer is compared to other systems.
- ============================
- EXAMPLE OUTPUT/RESULTS:
- (RASPBERRY PI B 256MB)
- Result of fib(40):
- CALCULATING...
- 165580141
- Benchmarking....
- 1/8...
- 2/8...
- 3/8...
- 4/8...
- 5/8...
- 6/8...
- 7/8...
- 8/8...
- =-=-=-=-=-=-=-=-=-=
- CALCULATING SCORES....
- 80.623535
- 82.071993
- 81.830015
- 80.193880
- 80.741247
- 84.901833
- 81.720455
- =-=-=-=-=-=-=-=-=-=
- CALCULATING AVERAGE....
- =-=-=-=-=-=-=-=-=-=
- =-=-=-=-=-=-=-=-=-=
- Final Score (lower is better)
- 81.726137
- =-=-=-=-=-=-=-=-=-=
- (CUSTOM PC UBUNTU I3-2100 4GB RAM)
- james@james-H67MA-D2H-B3:~$ ./benchmark
- CPU Model:
- Intel(R) Core(TM) i3-2100 CPU @ 3.10GHz
- Result of fib(40):
- Calculating....
- 165580141
- Benchmarking....
- 1/8...
- 2/8...
- 3/8...
- 4/8...
- 5/8...
- 6/8...
- 7/8...
- 8/8...
- =-=-=-=-=-=-=-=-=-=
- Calculating Scores....
- 6.576176
- 6.553473
- 6.538544
- 6.538626
- 6.532127
- 6.535168
- 6.531120
- Calculating Average....
- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=
- Final Score (lower is better)
- 6.543605
- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=
- (BELLARD.ORG/JSLINUX/)
- 4953.692964
- */
- //START PROGRAM
- #include <stdio.h>
- // sys/time.h and /sys/resource.h may not work on windows
- #include <sys/time.h>
- #include <sys/resource.h>
- //Timer setup
- //get_time MAY not work for windows.
- double get_time()
- {
- struct timeval t;
- struct timezone tzp;
- gettimeofday(&t, &tzp);
- return t.tv_sec + t.tv_usec*1e-6;
- }
- //fibonacci number function
- int fib(n)
- {
- if (n < 2)
- return 1;
- return fib(n-2) + fib(n-1);
- }
- //MAIN
- int main(int argc, char const* argv[])
- {
- //commment this section out if you are not compiling for linux
- //============================================================
- printf("CPU Model:\n");
- //Get CPU model
- int n;
- char line[1024], *p;
- FILE *f;
- if (!(f = fopen("/proc/cpuinfo", "r"))) //reads from /proc/cpuinfo and finds the "model_name" line
- err(1, "fopen");
- n = 5;
- while (n--)
- if(!fgets(line, sizeof(line), f))
- err(1, "fgets");
- fclose(f);
- n = strlen(line);
- line[--n] = '\0';
- if ((p = strchr(line, ':')) && (p + 2 - line) < n)
- fputs(p + 2, stdout);
- //finish get cpu model
- //==============================================================
- printf("\n\nResult of fib(40):\nCalculating....\n");
- //REMOVED BECAUSE CAUSED INACCURACIES
- //double time = get_time(); //gets the time
- printf("%d\n\n", fib(40)); //calculates and prints fib(40)
- //double time2 = get_time(); //gets time took to calculate
- //END
- printf("Benchmarking....\n");
- printf("1/8...\n");
- double time3 = get_time();
- fib(40);
- double time4 = get_time();
- printf("2/8...\n");
- double time5 = get_time();
- fib(40);
- double time6 = get_time();
- printf("3/8...\n");
- double time7 = get_time();
- fib(40);
- double time8 = get_time();
- printf("4/8...\n");
- double time9 = get_time();
- fib(40);
- double time10 = get_time();
- printf("5/8...\n");
- double time11 = get_time();
- fib(40);
- double time12 = get_time();
- printf("6/8...\n");
- double time13 = get_time();
- fib(40);
- double time14 = get_time();
- printf("7/8...\n");
- double time15 = get_time();
- fib(40);
- double time16 = get_time();
- printf("8/8...\n");
- double time17 = get_time();
- fib(40);
- double time18 = get_time();
- //
- //Calculates results (time & averages)
- //
- // printf("Score (lower is better):\n");
- printf("=-=-=-=-=-=-=-=-=-=\n");
- printf("Calculating Scores....\n");
- //
- // printf("%f\n", time2*8 - time*8); //prints
- //double score1 = time2*8 - time*8; //gets time taken to calculate * 2
- //
- printf("%f\n", time4*8 - time3*8);
- double score2 = time4*8 - time3*8;
- //
- printf("%f\n", time6*8 - time5*8);
- double score3 = time6*8 - time5*8;
- //
- printf("%f\n", time8*8 - time7*8);
- double score4 = time8*8 - time7*8;
- //
- printf("%f\n", time10*8 - time9*8);
- double score5 = time10*8 - time9*8;
- //
- printf("%f\n", time12*8 - time11*8);
- double score6 = time12*8 - time11*8;
- //
- printf("%f\n", time14*8 - time13*8);
- double score7 = time14*8 - time13*8;
- //
- printf("%f\n", time16*8 - time15*8);
- double score8 = time16*8 - time15*8;
- //
- printf("\n");
- printf("Calculating Average....\n");
- double average = score2 + score3 + score4 + score5 + score6 + score7 + score8; //calculates MEAN
- double final = average / 7; //continued
- printf("\n");
- printf("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n");
- printf("Final Score (lower is better)\n");
- printf("%f\n", final); //prints final result
- printf("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n");
- //printf("%d\n", get_time());
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement