Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <sys/time.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <wait.h>
- #define NUM_OF_CORES sysconf(_SC_NPROCESSORS_ONLN)
- #define MAX_PRIME 150000
- void do_primes(int coreNum)
- {
- int i, num, primes = 0;
- struct timeval startP, endP;
- long run_timeP, secondsP, usecondsP;
- gettimeofday(&startP, NULL);
- for (num = 1; num <= MAX_PRIME; num++)
- {
- for (i = 2; (i <= num) && (num % i != 0); i++);
- if (i == num)
- {
- primes++;
- }
- }
- gettimeofday(&endP, NULL);
- secondsP = endP.tv_sec - startP.tv_sec;
- usecondsP = endP.tv_usec - startP.tv_usec;
- run_timeP = ((secondsP) * 1000 + usecondsP / 1000.0) + 0.5;
- printf("\033[1;32m[ OK ]\033[0m Core #%d finished test...\n", coreNum);
- printf("\033[1m[INFO]\033[0m Core #%d finished in %ldms\n", coreNum, run_timeP);
- }
- int main(int argc, char ** argv)
- {
- system("clear");
- printf("\033[1;32m[ OK ]\033[0m Initializing...\n");
- printf("\n============\033[1m[ System Info ]\033[0m==============\n");
- printf("\033[1m[INFO]\033[0m CPU name: ");
- fflush(stdout);
- 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/@.*//'");
- printf("\033[1m[INFO]\033[0m CPU speed: ");
- fflush(stdout);
- system("grep -i --color 'model name' /proc/cpuinfo | uniq | sed -e 's/.*@ //'");
- printf("\033[1m[INFO]\033[0m # of CPU cores: %ld\n", NUM_OF_CORES);
- printf("\033[1m[INFO]\033[0m Test number: %d\n", MAX_PRIME);
- printf("\n===========\033[1m[ Starting Test ]\033[0m=============\n");
- printf("\033[1;32m[ OK ]\033[0m Starting tests...\n");
- struct timeval start1, end1;
- long run_time, seconds, useconds;
- gettimeofday(&start1, NULL);
- unsigned long i;
- pid_t pids[NUM_OF_CORES];
- for (i = 0; i < NUM_OF_CORES; i++)
- {
- if (!(pids[i] = fork()))
- {
- do_primes(i);
- exit(0);
- }
- if (pids[i] < 0)
- {
- perror("Fork");
- exit(1);
- }
- }
- for (i = 0; i < NUM_OF_CORES; i++)
- {
- waitpid(pids[i], NULL, 0);
- }
- gettimeofday(&end1, NULL);
- printf("\033[1;32m[ OK ]\033[0m Test done, calculating results...\n");
- seconds = end1.tv_sec - start1.tv_sec;
- useconds = end1.tv_usec - start1.tv_usec;
- run_time = ((seconds) * 1000 + useconds / 1000.0) + 0.5;
- printf("\n==============\033[1m[ Results ]\033[0m================\n");
- printf("\033[1m[INFO]\033[0m Time taken: %ldms", run_time);
- printf("\n\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment