Random and Time

S

shapper

Hello,

Is it possible to create an array of 10 000 random doubles and measure
how much time does it take?

I need to test the performance of some calculations but I am not sure
how ...

Thank You,
Miguel
 
S

shapper

See the Stopwatch class.  It's the most straightforward way.  Make sure  
you run your test with enough trials that you get a statistically useful  
time measurement.  I.e. start the Stopwatch instance, and then inside a 
loop with a large number of iterations, execute the code you want to  
measure, stopping the Stopwatch immediately at the end of the outer  
iteration loop.  Divide the result by the number of iterations to get a 
good estimate of the time cost of the code.

Also, in .NET especially, you'll get much better results if you execute  
the code of interest a bunch of times first, before starting timing (I've 
seen 100 iterations used as a basic strategy).

Pete

Thank You Pete.
I found the page in MSDN for the Stopwatch.

I understand that I should use many iterations.
It is just strange that I should start counting only after 100 ...
Why?
Is there a reason for this?
 
P

Pavel Minaev

I understand that I should use many iterations.
It is just strange that I should start counting only after 100 ...
Why?
Is there a reason for this?

You want to be sure that you don't count one-time expenses (such as
JIT), and also that everything that could be cached was cached (on all
levels, from libraries to CPU cache), if you're trying to measure the
actual workload you will see in practice.
 
C

Cor Ligthert[MVP]

In addition to the others.

Be aware that you seldom get a real answer on this because Windows is an
multi tasking operating system which is used for several other things than
your test.

I think that in Vista you will never get a clear result as that is doing all
kind of things, you are not aware of when your test is running.

However, that is one reason why you should do the test several times in a
loop.

Maybe it is an option now we have Vista to take the shortens elapsed time
and not the sum of the times divided by the loops.

Cor
 
C

Cor Ligthert[MVP]

In addition to the others.

Be aware that you seldom get a real answer on this because Windows is an
multi tasking operating system which is used for several other things than
your test.

I think that in Vista you will never get a clear result as that is doing all
kind of things, you are not aware of when your test is running.

However, that is one reason why you should do the test several times in a
loop.

Maybe it is an option now we have Vista to take the shortens elapsed time
and not the sum of the times divided by the loops.

Cor
 

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

Similar Threads


Top