## timing in milli-seconds

In this forum members can discuss topics about specific programming languages.
whakamaru
Posts: 47
Joined: Thu May 06, 2010 10:08 pm

### timing in milli-seconds

And I know about the tick count at low memory 0040:006c, but that only works at 18.2 'ticks' per second. (about 50 ms)
Does someone know where the millisecond count is in memory?

otac0n
Posts: 13
Joined: Mon Jun 14, 2010 5:55 pm

### Re: timing in milli-seconds

There is probably a better way of doing what you are thinking about...

Generally speaking, in order to get a better view of time than the tickcount on a windows machine, you need to access the "high-resolution system timers".

What language are you using?

If you are using a .NET language, you have access to the Stopwatch class, and DateTime.Now (both of which use the high-resolution timers as their source).

If not, (it looks like you may be using ASM), you need to call out to the Win32 API function QueryPerformanceCounter.

http://msdn.microsoft.com/en-us/library/ms644904.aspx

Posts: 13
Joined: Mon Mar 14, 2011 12:45 pm

### Re: timing in milli-seconds

whakamaru wrote:Does someone know where the millisecond count is in memory?
Here is something I've used in a few C programs I've written on Windows:

htime.h:

Code: Select all

#ifndef HTIME_H
#define HTIME_H

#include <windows.h>

typedef struct {
LARGE_INTEGER start;
LARGE_INTEGER stop;
} stopwatch;

void start_timer(stopwatch *timer);
void stop_timer(stopwatch *timer);
double secs_from_li(LARGE_INTEGER *li);
double elapsed_time(stopwatch *timer);

#endif
htime.c:

Code: Select all

#include "htime.h"

void start_timer(stopwatch *timer) {
QueryPerformanceCounter(&timer->start);
}

void stop_timer(stopwatch *timer) {
QueryPerformanceCounter(&timer->stop);
}

double secs_from_li(LARGE_INTEGER *li) {
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
}

double elapsed_time(stopwatch *timer) {
LARGE_INTEGER time;
return secs_from_li(&time);
}
And here's an example of using it:

Code: Select all

#include <stdio.h>
#include "htime.h"

int main(void) {
stopwatch sw;
long long i, total = 0;

start_timer(&sw);

// add the numbers from 1 to 1,000,000
for (i = 1; i < 1000000; i++) {
total += i;
}

stop_timer(&sw);

printf("%lld\n", total);
printf("%1.6f seconds\n", elapsed_time(&sw));

return 0;
}
Sample output:

Code: Select all

$./foo 499999500000 0.001075 seconds In Java, I would just use System.currentTimeMillis() or System.nanoTime(). In C#, as otac0n mentioned, you can use System.Diagnostics.Stopwatch: Code: Select all using System; using System.Diagnostics; public class Foo { public static void Main(string[] args) { Stopwatch sw = Stopwatch.StartNew(); long total = 0; for (long i = 1; i < 1000000; ++i) { total += i; } Console.WriteLine(total); Console.WriteLine(sw.Elapsed); } } Output: Code: Select all $ ./foo
499999500000
00:00:00.0022043
Hope this is helpful to the OP, or anyone else.

Cheers,

Francky
Posts: 90
Joined: Sat May 07, 2011 2:49 pm
Location: South of France

### Re: timing in milli-seconds

Here is my chrono decorator for Python

Code: Select all

from time import time

def chrono(n=3):
def mon_decorateur(f):
def mon_wrapper(*args, **kwargs):
t=time()
for _ in range(n):
st=time()
res=f(*args, **kwargs)
st=time()-st
t=min(t, st)
print(res, t) # for Python 3
# print res, t # for Python 2
return mon_wrapper
return mon_decorateur

And how I use it :

Code: Select all

@chrono()  # best time in 3 executions
def foo(lim=10**13)
for i in range(lim): y=1+i
return 'my result'

foo() # for lim=10**13

foo(10**15) # for lim=10**15

@chrono(10) # for the best time in 10 executions
def bar(): #...

bar()

I get 'my result' and next to the best execution time of three tests.

------

I would like to see C++ solutions with Boost librairies,
it would be kind, thanks. I'm tring to learn a little.
I tried something, but with -O3 option in compilation, the compilo "cheated"
if I ran 1000 times the same algo, it didn't do it.

It helps me to understand how some variations are good or not.
It's difficult when algo are under a milli second.
Entia non sunt multiplicanda praeter necessitatem

hk
Posts: 10702
Joined: Sun Mar 26, 2006 9:34 am
Location: Haren, Netherlands

### Re: timing in milli-seconds

Compilers sometimes "know" that a result is not used and while compiling eliminate parts of code that calculate results that are not used.
So if anything: do something with the result; e.g. add it into a variable that is printed after the testing loop.
You will actually also be timing the adding of the result, but it is a way of fooling the optimiser.

drwhat
Posts: 41
Joined: Tue Sep 06, 2011 3:56 am

### Re: timing in milli-seconds

Here is a C/C++ method that I believe will work with just about any compiler/environment for windows

Code: Select all

#include "time.h"

// inside the main function
clock_t start, finish;
double duration;

start=clock();

finish = clock();
duration = (double) (finish-start) / CLOCKS_PER_SEC;
cout << "Done with Program in: " << duration << " seconds" << endl;


whakamaru
Posts: 47
Joined: Thu May 06, 2010 10:08 pm

### Re: timing in milli-seconds

I was surprised, (and delighted) when my solution for #303 displayed an 11-digit number after just a few seconds. But how fast? Manual timing with a stop-watch showed 1.81 seconds, but that includes my reaction time from when the answer appeared, to when I stopped the watch.
Easy in a high-level language, not so easy in ASM
So I wrote a little program that displays the now time - seconds and hundredths. (but if f(2c) int 21 uses the tick count it will only be accurate to .05s?)
Then remembered the DOS days, so wrote a three-line batch program
mytime.com
pe303.com
mytime.com
and then TIMEIT.BAT worked, another surprise, I thought MS had disconnected itself from DOS
it showed
32.51