C++ get time in milliseconds

S

Saurabh

Hi ,

i am working with g++ on Linux.is there a way in C++ to get current
time in milliseconds.( <ctime> provides accuracy upto seconds).
I googled for it,searched the newsgroup but couldnt find anything.
i hope i will get the help
thanks & regards
Saurabh
 
M

Mehturt

Saurabh said:
Hi ,

i am working with g++ on Linux.is there a way in C++ to get current
time in milliseconds.( <ctime> provides accuracy upto seconds).
I googled for it,searched the newsgroup but couldnt find anything.
i hope i will get the help
thanks & regards
Saurabh

gettimeofday()
 
W

wallaby.curious.about.it

Saurabh said:
Hi ,

i am working with g++ on Linux.is there a way in C++ to get current
time in milliseconds.( <ctime> provides accuracy upto seconds).
I googled for it,searched the newsgroup but couldnt find anything.
i hope i will get the help
thanks & regards
Saurabh

Hi,

You can use clock_t from standard library to measure short intervalls
of time. from a millisec to some seconds. You can convert it to seconds
with CLOCKS_PER_SEC macro.
example:
#include<ctime>
#include<time.h> // I don't know which1 exactly :)
clock_t t1=clock();
for(int i=0; i<1000000;i++);
clock_t t2=clock();
cout<<"Iterations took for" << doubel(t2-t1)/CLOCK_PER_SEC << "seconds";
 
M

Markus Schoder

Hi,

You can use clock_t from standard library to measure short intervalls
of time. from a millisec to some seconds. You can convert it to seconds
with CLOCKS_PER_SEC macro.
example:
#include<ctime>
#include<time.h> // I don't know which1 exactly :)
clock_t t1=clock();
for(int i=0; i<1000000;i++);
clock_t t2=clock();
cout<<"Iterations took for" << doubel(t2-t1)/CLOCK_PER_SEC << "seconds";

clock() measures processor time however.
 
J

Jonathan Mcdougall

Saurabh said:
Hi ,

i am working with g++ on Linux.is there a way in C++ to get current
time in milliseconds.( <ctime> provides accuracy upto seconds).
I googled for it,searched the newsgroup but couldnt find anything.

You did? Searching for "time millisecond" in comp.lang.c++ gives some
pretty good results.

Time and date features are found in <ctime>. However, what the accuracy
of time() is implementation defined. Implementations are not even
required to provide a working time() function.

That <ctime> "provides accuracy up to seconds" is wrong, I think. POSIX
requires CLOCKS_PER_SEC to be 1,000,000 (microseconds) and Windows has
1000 (millisecond, on Visual C++ 2005).

You may be better served by platform specific features if you require
more precise timing. See
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9 for
newsgroup suggestions.


Jonathan
 
O

osmium

Jonathan Mcdougall said:
Saurabh wrote:

You did? Searching for "time millisecond" in comp.lang.c++ gives some
pretty good results.

Google and Google groups are two entirely different worlds. Which I, and I
suspect a lot of seasoned users, forget from time to time.
 
J

Jonathan Mcdougall

osmium said:
Google and Google groups are two entirely different worlds.

They index different things but they are "search engines", as far as I
know. Search thoroughly or don't search at all.

I rarely suggest posters to STFW, but I do rant a bit when I see "I did
searched for it, but couldn't find anything".
Which I, and I
suspect a lot of seasoned users, forget from time to time.

Does that apply to me? The OP specifically said "I googled for it,
searched the newsgroup but couldn't find anything".


Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top