Hack Forums
[C] C++ Timer - How can I make this more efficiently? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] C++ Timer - How can I make this more efficiently? (/showthread.php?tid=4481573)



C++ Timer - How can I make this more efficiently? - Silent Sweat - Oct 8, 2014

I know this is probably the most shit program you've ever seen, but it is all I am currently capable of. This was made completely from scratch, without looking anything up. It took me about 20 minutes (had some problems with the logic) but I got it to work technically like a timer, although it is very inefficient (even I know this).

But I don't know how to make it like a C++ pro would.. any suggestions on improvement?

Here is the pastebin code source: HERE


* - topLevel - Oct 8, 2014

At first thought, use boost it has a simple way to manage time.
edit..
http://www.boost.org/doc/libs/1_55_0/doc/html/date_time/posix_time.html#date_time.posix_time.time_duration


* - Qmz_ - Oct 8, 2014

...or

PHP Code

<?php 
clock_t start 
= clock();
// ...
clock_t end = clock();
double elapsed = double(end - start) / CLOCKS_PER_SEC; 



* - Silent Sweat - Oct 8, 2014

(Oct 8, 2014 06:35 PM)Qmz_ Wrote:  ...or

PHP Code

<?php 
clock_t start 
= clock();
// ...
clock_t end = clock();
double elapsed = double(end - start) / CLOCKS_PER_SEC; 

Can you elaborate on that? Please, I am a beginner. Explain to me what that does, and how I can replace my code with it.


* - 0xDEAD10CC - Oct 8, 2014

(Oct 8, 2014 07:42 PM)Paranova Wrote:  Can you elaborate on that? Please, I am a beginner. Explain to me what that does, and how I can replace my code with it.

What is there to elaborate on? The code is self explanatory. It takes the current measurement of time, // ... indicates some kind of code that takes time to execute, and an end measurement of time is stored in a variable. To find the time elapsed, it's the delta between these 2 calculated values, and the macro CLOCKS_PER_SEC is a defined value for the "clocks per second", thus elapsed is a measurement of elapsed seconds.