0

I'm trying to design a code in C for a class that will tell me how long it takes to run a function in milliseconds. My professor suggested that I use chrono to do this, but I can't get chrono to work on my system. When I try something like the code below, I get "fatal error: chrono: No such file or directory."

#include <chrono>
int main()
{
   return 0;
} 

I have tried the following compilier commands, but they aren't working

gcc prog.c

or

gcc -std=c++11 prog.c

(This works if I change the program to prog.cc but my professor requires that all programs be .c).

I am using PuTTY provided by my school, a gcc compiler, and C as the programming language.

Please help!

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Rosie13
  • 11

1 Answers1

1

<chrono> is a C++ library - you can't use that for your code if you're compiling with gcc, but you could probably make it work if you compile with g++. (Note that this compiler is more picky; your C code will likely need some editing if it's more than just an empty main().)

Maybe use <time.h> instead? It contains several similar functions related to time keeping.

Nick Reed
  • 4,989
  • 4
  • 17
  • 37
  • Can time.h be used to find the time in milliseconds? My professor says the output has to be in milliseconds. – Rosie13 Oct 19 '19 at 03:40
  • Sure, it works in milliseconds too, [according to this answer.](https://stackoverflow.com/a/10192994/7431860) – Nick Reed Oct 19 '19 at 03:47
  • @NickReed -- that answer refers to and gettimeofday, which is POSIX specific. (part of the C standard) only has time (giving time in seconds) and clock (giving clock ticks, with unspecified accuracy), and only in C11 adds timespec_get which gives time in nanoseconds. – Chris Dodd Oct 19 '19 at 19:42