Nov 23, 2009

C++ Time

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Programming General > C, C++ & Visual C++

C++ Time

sparkx
How do I get the current time (in milliseconds or better) using C++? I need to find the difference between two intervals in time so my program can adjust so it won’t be real fast on some computer and real slow on others.
I have tried time() from ctime.h. It turns out that it is not accurate at all (as it seems to round to the nearest second, which is not good enough for my program).
I have also tried clock() then dividing by CLOCKS_PER_SEC but doing this worries me because it is a macro and is getting the CLOCKS_PER_SEC of the computer that compiled the program, not the one that is running it.
Is there another option that works better?

Thanks,
Sparkx




Comment/Reply (w/o sign-up)

Quatrux
I think you can try to search google, but I didn't do much of C++ and didn't test this, but if on Windows OS, you can use timeGetTime(); I guess..

you need to include as I remember <windows.h> or something like that and use timeGetTime(); which will return time in milliseconds. smile.gif I think it's very usable using C++ and DirectX on Windows, on a simple console applications I never really used any functions to get time.

Comment/Reply (w/o sign-up)

Lordrach
hi,

I suggest you to use the free and open source SDL library, i have used it for 2d game programming and i can say it's really a nice API (2D, images, networking, Audio, fonts).

for time management you can use the SDL_GetTicks function witch returns the number of milliseconds since SDL library initialization.

the SDL library offers another way to managing time witch is the timers.

you can find more information about time management in SDL here

Comment/Reply (w/o sign-up)

sparkx
Thanks for the replies, I think timeGetTime() is the one I am looking for. By the way I was wondering, is there a way to debug a program to see how long each thing takes to execute. Before I was just using the sleep() function to update the time every 10 milliseconds then calling the time throughout the rest of the program (which works for development but I'm not sure how it works for releases). This seems like it should work better then anything else unless the total execution time is significantly larger then 10 milliseconds (of coarse it should be a little longer then 10 milliseconds because of the sleep and execution but that is why I am using intervals of 10 not 1 keeping my program more accurate). Is it method valid?

Thanks,
Sparkx

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics
Looking for C++, Time

See Also,

*SIMILAR VIDEOS*
Searching Video's for C++, Time
advertisement



C++ Time

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com