Created
December 8, 2023 14:44
-
-
Save wrey75/9caee904529bdbcd82621d1a8b58fb48 to your computer and use it in GitHub Desktop.
A prime calculator to test the speed of a server
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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
#include <math.h> | |
#include <time.h> | |
/** | |
* Defined as a default. You can pass any value as first parameter. | |
*/ | |
#define LIMIT (200000) | |
/* | |
* This software is not useful to calculate the prime numbers | |
* but really a good method to test the speed of a CPU. Note | |
* the program should work on a single core and this could | |
* modify the speed perception. | |
* | |
* How to use it? | |
* cc primes.c && time ./a.out | |
* | |
* This will give you the CPU usage used by the software. It | |
* is a very fast and very simple way to check the speed of | |
* a VPS (like Amazon EC2 or any other provider). | |
*/ | |
int isPrime(int input){ | |
for(int i = 2; i < input - 1; i++){ | |
if( input % i == 0) return false; | |
} | |
return true; | |
} | |
int main(int argc, char **argv) { | |
time_t rawtime; | |
int limit = LIMIT; | |
if(argc > 1){ | |
limit = atoi(argv[1]); | |
} | |
time_t previous = time(&rawtime); | |
int last = 2; | |
for (int i = 2; i < limit; i++) { | |
if(isPrime(i)){ | |
last = i; | |
time(&rawtime); | |
if(rawtime > previous){ | |
printf("%d...\n", last); | |
previous++; | |
} | |
} | |
} | |
printf("%d (done)\n", last); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment