How to measure time a function spends in "user space" on Win32

R

Robbo

Hi,
I need to measure the time a function spends in "user space" on Win32.


I thought to use clock() function:

clock_t start_clock=clock();
funzione();

clock_t end_clock=clock();

double time = (double)(end_clock-start_clock) / CLOCKS_PER_SEC;

printf("%f\n",time);

In your opinion, is it correct?
What other methods do you suggest me?

Thank you.
 
K

Keith Thompson

Robbo said:
I need to measure the time a function spends in "user space" on Win32.


I thought to use clock() function:

clock_t start_clock=clock();
funzione();

clock_t end_clock=clock();

double time = (double)(end_clock-start_clock) / CLOCKS_PER_SEC;

printf("%f\n",time);

In your opinion, is it correct?
What other methods do you suggest me?

The clock() function measure processor time, not wall clock time. If
that's what you want to measure, then it's just the thing; if not, it
isn't.

The only alternative C provides is the time() function, which measures
wall clock time -- but the granularity, though it's not specified by
the language, is 1 second on most systems (including Win32 if I'm not
mistaken).

It depends on what you mean by "user space". It's likely that you'll
need to use some Win32-specific method; if so, try asking in
comp.os.ms-windows.programmer.win32.
 
R

Robbo

The clock() function measure processor time, not wall clock time.  If
that's what you want to measure, then it's just the thing; if not, it
isn't.

The only alternative C provides is the time() function, which measures
wall clock time -- but the granularity, though it's not specified by
the language, is 1 second on most systems (including Win32 if I'm not
mistaken).

It depends on what you mean by "user space".  It's likely that you'll
need to use some Win32-specific method; if so, try asking in
comp.os.ms-windows.programmer.win32.

--
Keith Thompson (The_Other_Keith) (e-mail address removed)  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

With "user space" time I mean the time the process spends in "user
space" during the execution of my function.
So, if the function execution is interrupted by the operative system,
for instance because anti-virus update is executed, the time counter
stops.
 
K

Keith Thompson

Robbo said:
With "user space" time I mean the time the process spends in "user
space" during the execution of my function.
So, if the function execution is interrupted by the operative system,
for instance because anti-virus update is executed, the time counter
stops.

I'm still not 100% certain what you mean by "user space". On some
systems, there's a distinction between "user" and "kernel" spaces. If
a program executes a system call, work is performed by the kernel on
the program's behalf; that time will probably count against the
program's CPU time, but is counted separately from time spent in the
program itself (doing computations, for example); clock() will most
likely reflect the sum of both times. On the other hand, if the
program is interrupted to allow other programs to run, that time isn't
counted against your program.

If your program spends 2 seconds running its own computations, 1
second waiting for the OS to handle system calls made by the program,
and 4 seconds waiting while other programs run, time() should indicate
a total of 7 seconds and clock() should indicate a total of 3 seconds.
If the 3-second figure is what you're looking for, then clock() is
probably what you want. But if you're not certain of this, then I
suggest again that you ask in comp.os.ms-windows.programmer.win32.
 
R

Robbo

I'm still not 100% certain what you mean by "user space".  On some
systems, there's a distinction between "user" and "kernel" spaces.  If
a program executes a system call, work is performed by the kernel on
the program's behalf; that time will probably count against the
program's CPU time, but is counted separately from time spent in the
program itself (doing computations, for example); clock() will most
likely reflect the sum of both times.  On the other hand, if the
program is interrupted to allow other programs to run, that time isn't
counted against your program.

If your program spends 2 seconds running its own computations, 1
second waiting for the OS to handle system calls made by the program,
and 4 seconds waiting while other programs run, time() should indicate
a total of 7 seconds and clock() should indicate a total of 3 seconds.
If the 3-second figure is what you're looking for, then clock() is
probably what you want.  But if you're not certain of this, then I
suggest again that you ask in comp.os.ms-windows.programmer.win32.

--
Keith Thompson (The_Other_Keith) (e-mail address removed)  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Ok, I am looking for the 2-second figure. I am going to ask on
comp.os.ms-windows.programmer.win32.

Thank you.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top