brickmasterj

Linux C CPU Benchmark

Oct 9th, 2012
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <sys/time.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <wait.h>
  8.  
  9. #define NUM_OF_CORES sysconf(_SC_NPROCESSORS_ONLN)
  10. #define MAX_PRIME 150000
  11.  
  12. void do_primes(int coreNum)
  13. {
  14.     int i, num, primes = 0;
  15.     struct timeval startP, endP;
  16.     long run_timeP, secondsP, usecondsP;    
  17.     gettimeofday(&startP, NULL);
  18.    
  19.     for (num = 1; num <= MAX_PRIME; num++)
  20.     {
  21.         for (i = 2; (i <= num) && (num % i != 0); i++);
  22.        
  23.         if (i == num)
  24.         {
  25.             primes++;
  26.         }
  27.     }
  28.    
  29.     gettimeofday(&endP, NULL);
  30.     secondsP  = endP.tv_sec - startP.tv_sec;
  31.     usecondsP = endP.tv_usec - startP.tv_usec;
  32.     run_timeP = ((secondsP) * 1000 + usecondsP / 1000.0) + 0.5;
  33.    
  34.     printf("\033[1;32m[ OK ]\033[0m Core #%d finished test...\n", coreNum);
  35.     printf("\033[1m[INFO]\033[0m Core #%d finished in %ldms\n", coreNum, run_timeP);
  36. }
  37.  
  38. int main(int argc, char ** argv)
  39. {
  40.     system("clear");
  41.     printf("\033[1;32m[ OK ]\033[0m Initializing...\n");
  42.     printf("\n============\033[1m[ System Info ]\033[0m==============\n");
  43.     printf("\033[1m[INFO]\033[0m       CPU name: ");
  44.     fflush(stdout);
  45.     system("grep -i --color 'model name' /proc/cpuinfo | uniq | sed -e 's/model name//' | sed -e 's/: //' | sed -e 's/          //'| sed -e 's/ //' | sed -e 's/@.*//'");
  46.     printf("\033[1m[INFO]\033[0m      CPU speed: ");
  47.     fflush(stdout);
  48.     system("grep -i --color 'model name' /proc/cpuinfo | uniq | sed -e 's/.*@ //'");
  49.     printf("\033[1m[INFO]\033[0m # of CPU cores: %ld\n", NUM_OF_CORES);
  50.     printf("\033[1m[INFO]\033[0m    Test number: %d\n", MAX_PRIME);
  51.     printf("\n===========\033[1m[ Starting Test ]\033[0m=============\n");
  52.     printf("\033[1;32m[ OK ]\033[0m Starting tests...\n");
  53.    
  54.     struct timeval start1, end1;
  55.     long run_time, seconds, useconds;    
  56.     gettimeofday(&start1, NULL);
  57.    
  58.     unsigned long i;
  59.     pid_t pids[NUM_OF_CORES];
  60.    
  61.     for (i = 0; i < NUM_OF_CORES; i++)
  62.     {
  63.         if (!(pids[i] = fork()))
  64.         {
  65.             do_primes(i);
  66.             exit(0);
  67.         }
  68.        
  69.         if (pids[i] < 0)
  70.         {
  71.             perror("Fork");
  72.             exit(1);
  73.         }
  74.     }
  75.    
  76.     for (i = 0; i < NUM_OF_CORES; i++)
  77.     {
  78.         waitpid(pids[i], NULL, 0);
  79.     }
  80.    
  81.     gettimeofday(&end1, NULL);
  82.    
  83.     printf("\033[1;32m[ OK ]\033[0m Test done, calculating results...\n");
  84.    
  85.     seconds  = end1.tv_sec - start1.tv_sec;
  86.     useconds = end1.tv_usec - start1.tv_usec;
  87.     run_time = ((seconds) * 1000 + useconds / 1000.0) + 0.5;
  88.    
  89.     printf("\n==============\033[1m[ Results ]\033[0m================\n");    
  90.     printf("\033[1m[INFO]\033[0m     Time taken: %ldms", run_time);
  91.    
  92.     printf("\n\n");    
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment