Find Prime Numbers using time.h in C

AI Thread Summary
The discussion revolves around implementing algorithms to find prime numbers up to a specified integer in C, utilizing the Sieve of Eratosthenes and an isprime() function. The user seeks to measure the execution time of these algorithms accurately, noting that printing results to the screen may skew timing results. Suggestions include using the clock() function for timing, but it may not provide the precision needed for fast computations. Alternatives like profiling tools and high-performance timers are recommended for more accurate measurements. The user plans to refine their code based on the feedback and share a functional solution later.
Kalouste
Messages
21
Reaction score
0
I've written an algorithm that has the following goal: finding all prime numbers up to a specified integer. I've made two different algorithms actually: on one hand, I've used the concept beyond the ancient sieve of eratosthenes; on the other, I've used a function called isprime() that tests if a number is prime. For instance, let's say I want to determine which method is faster. So I need to time a piece of code in each algorithm.

As an example, here's the code of the first algorithm:

Code:
#include <stdio.h>
#include <math.h>
#include <time.h>

int isprime(int num);

int main () {
int n, num;
time_t t1, t2;

printf("Enter a number\n");

while (scanf("%d", &n) != 1) {
printf("Error: Try again.\n");
	while (getchar() != '\n')
		;
		}

printf("Prime numbers\n");

time(&t1);

for (num = 2; num <= n; num += 1) {
if (isprime(num) == 1)
printf("%d\n", num);

}

time(&t2); 
printf("Time elapsed: %f ms\n", (float)(t2-t1));

return 0;
}

I've omitted the isprime() function definition, because I felt it's unnecessary. The problem with this approach is that my computer writes the numbers so rapidly that I probably will need to write the time in milliseconds if I wanted the algorithm to write every prime number up to a small number, as 11. Is there a function that allows me to do that?
Thank you on advance.

Links I've consulted

"[URL
 
Last edited by a moderator:
Technology news on Phys.org
There is the clock() function. But if you want really accurate timing, your best bet is to do one of the following: (I think (1) is the most reliable)

(1) Wrap your code in a loop and repeat it a few thousand times (or million, as necessary), so that the elapsed time is measured in seconds or minutes.

(2) Use a profiling tool.

(3) Try and find an API for the high-performance timer provided by your operating system.



Incidentally, there are two problems with your code:
(1) Printing the primes to screen is probably more time-consuming than the calls to isprime().

(2) On the final printf(...) call, you told it to print an int value (%d), but passed it a float.
 
Note though that the clock() function measures the cpu time not the elapsed time.
 
(2) On the final printf(...) call, you told it to print an int value (%d), but passed it a float.
Sorry for that, didn't notice.

About the clock() function I will try to use it. I found an interesting thread about that function http://www.thescripts.com/forum/thread213478.html" . When I come up with a fully functional solution I will post it. The (3) solution will also be considered. I didn't know about the existence of 'profiling tools', so I just learned something. Thank you for the suggestions.
 
Last edited by a moderator:
you have figured out by now, if not maybe this can help

Code:
#include <ctime>

clock_t cstart = clock();
...
clock_t cend = clock();

double millis = 1000.0 * (cend - cstart) / CLOCKS_PER_SEC;
 
The clock() function returns, as MeJennifer stated before, the cpu time which is less than the elapsed time (recall Hurkyl's post: 'Printing the primes to screen is probably more time-consuming than the calls to isprime()'). So I tried this solution, which seems to work although it's not extremely accurate, as one could desire (nor is the clock() function: it returns 0.00000 s cpu time if the computation is done too fast):

Code:
#include <sys/times.h>
#include <sys/wait.h>

  static clock_t st_time;
  static clock_t en_time;
  static struct tms st_cpu;
  static struct tms en_cpu;

  st_time = times(&st_cpu);
  ...
  en_time = times(&en_cpu);

printf("Time elapsed: %ld ms\n", (en_time - st_time));

Example: Print all primes up to 200.
Cpu_time: 0.000000 s
Time elapsed: 0 s (using <time.h>)
Time elapsed: 15 ms (using struct)

http://www.codeproject.com/csharp/highperformancetimercshar.asp"
 
Last edited by a moderator:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top