Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » How much time

Credits go to Johan Halmén for helping out!
This thread is locked; no one can reply to it. rss feed Print
How much time
Decelion
Member #8,193
January 2007

Hi!

I'm new to allegro and now I have the first problem that I can't bring away myself.
So my question is very simple:
How can I read the time something takes?
example:
I have the following code:

//Here I want to start the timer

//...code...

//and here I want to know how much time is left since I started the timer.

How can I do this?
I looked in the Manual and the forum search but I did not find anything what helped me. (Maybe I did not find it because of my English...)

Thank you,

Decelion

PS: Sorry for my English being not very good sometimes, I am from Germany. If you find mistakes, please tell me.

ixilom
Member #7,167
April 2006
avatar

You can use the clock() function from time.h to check the duration.

clock_t start = clock();
clock_t end;
/*
Do whatever you need to check the duration of here
*/
end = clock();
// Calculate the duration in seconds
duration=(double)(stop-start)/CLOCKS_PER_SEC;

___________________________________________
Democracy in Sweden? Not since 2008-Jun-18.
<someone> The lesbians next door bought me a rolex for my birthday.
<someone> I think they misunderstood when I said I wanna watch...

Johan Halmén
Member #1,550
September 2001

Well, you could use allegro timer stuff:

1// make this global
2unsigned int my_time;
3 
4// your timer function
5void my_timer_f(void)
6{
7 my_time++;
8}
9 
10 
11// f.i. in main()
12install_timer();
13install_int(my_timer_f, 1); // Your timer function will run once every millisecond
14my_time = 0;
15 
16 
17 
18// then to your code:
19//Here I want to start the timer
20unsigned int start_time = my_time;
21 
22//...code...
23 
24//and here I want to know how much time is left since I started the timer.
25unsigned int time_used = my_time - start_time; // in milliseconds

What happens is you

  • initialize the timer stuff

  • create a function that increments a global int variable

  • set this function to run f.i. once every millisecond

  • read the global variable anytime you want

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

James Stanley
Member #7,275
May 2006
avatar

If you want to time how long the program takes to execute (which you probably don't. You're probably doing a fps count), you can execute it with time.

Instead of 'myprogram', you type 'time myprogram', and it will tell you the time at the bottom.

I don't think Windows has this, though, so if you want to be portable use Allegro timers.

EDIT:

Quote:

unsigned int my_time;

Doesn't it need to be volatile?

Decelion
Member #8,193
January 2007

Ok, thank you very much!
Now I can go on making Doom 4 ;)

Johan Halmén
Member #1,550
September 2001

Quote:

Doesn't it need to be volatile?

Yes, I think so. Just for sure. If you have heavy optimization on, your compiler might make your application reuse the memory space of the variable, if you don't declare it volatile.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

nonnus29
Member #2,606
August 2002
avatar

Quote:

Ok, thank you very much!
Now I can go on making Doom 4 ;)

L0L, a sense of humor is very important, good luck!

Paul whoknows
Member #5,081
September 2004
avatar

Profiling can be useful too.

____

"The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner.

Go to: