How much time
Decelion

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

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;

Johan Halmén

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

James Stanley

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

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

Johan Halmén
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.

nonnus29
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

Profiling can be useful too.

Thread #589462. Printed from Allegro.cc