0

quick question;

I'm using Ubuntu as my coding environment, and I am trying to write a C program for Windows for school.

The assignment says I have to do something using the system clock, and I decided to make a quick benchmarking program. Here it is:

#include <stdio.h>
#include <unistd.h> 
#include <time.h> 
int main () { 
  int i = 0;
  int p = (int) getpid(); 
  int n = 0; 
  clock_t cstart = clock(); 
  clock_t cend = 0;

  for (i=0; i<100000000; i++) { 
    long f = (((i+9)*99)%4)+(8+i*999);
    if (i % p == 0)
      printf("i=%d, f=%ld\n", i, f); 
  } 

  cend = clock();
  printf ("%.3f cpu sec\n", ((double)cend - (double)cstart)* 1.0e-6);
  return 0;
}

When I cross compile from Ubuntu to Windows using mingw32, it's fine. However, when I run the program in Windows, two issues happen:

The benchmark runs as expected, and takes roughly 5 seconds, yet the timer says it took 0.03 seconds (this doesnt happen when testing in my Ubuntu VM. If the benchmark takes 5 seconds in real time, the timer will say 5 seconds. So obviously, this is an issue.)

Then, once the program is done, the Windows terminal will close immediately.

How do I make the program stay open so you can look at your time for more than like 10 milliseconds, and how can I make the runtime of the benchmark reflect it's score like it does when I test in Ubuntu?

Thanks!

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
elemein
  • 197
  • 6
  • For preventing the program from closing: http://stackoverflow.com/questions/1173208/what-is-the-best-practice-for-combating-the-console-closing-issue – John Zwinck Sep 24 '14 at 01:34
  • 1
    You're assuming that `CLOCKS_PER_SEC` (the amount that clock() increases in one second) is equal to one million, which isn't true in Windows. Instead of multiplying by `1.0E-6`, you should divide by `CLOCKS_PER_SEC`. – Harry Johnston Sep 24 '14 at 01:59
  • I'm sorry, I don't quite understand what exactly to do, as I'm not familiar with the "CLOCKS_PER_SEC" function. If it isnt too much trouble, could you point me to how I would fashion a line of code to remedy the issue? – elemein Sep 25 '14 at 14:38

0 Answers0