| View previous topic :: View next topic |
| Author |
Message |
Gaijinco Guest
|
Posted: Thu Sep 29, 2005 3:18 am Post subject: How to measure code-efficiency? |
|
|
Every now and then and found two ways to resolve a problem. I try to
find a way to decide which one is best (at least speed-wise) but I
don't know how do I test how long those it take a program to run?
|
|
| Back to top |
|
 |
puzzlecracker Guest
|
Posted: Thu Sep 29, 2005 3:32 am Post subject: Re: How to measure code-efficiency? |
|
|
Gaijinco wrote:
| Quote: | Every now and then and found two ways to resolve a problem. I try to
find a way to decide which one is best (at least speed-wise) but I
don't know how do I test how long those it take a program to run?
|
I would you a regular timer (one of those that are used during
marathons): start in one hand, while you press enter with a different
one.
Good luck...
tell how it went
|
|
| Back to top |
|
 |
David White Guest
|
Posted: Thu Sep 29, 2005 4:23 am Post subject: Re: How to measure code-efficiency? |
|
|
Gaijinco wrote:
| Quote: | Every now and then and found two ways to resolve a problem. I try to
find a way to decide which one is best (at least speed-wise) but I
don't know how do I test how long those it take a program to run?
|
I time code execution like this:
#include <ctime>
// ...
clock_t t0 = clock();
// ---- insert code to be timed ----
double tElapsedSec = clock() - t0;
tElapsedSec /= CLOCKS_PER_SEC;
// ...
DW
|
|
| Back to top |
|
 |
Gaijinco Guest
|
Posted: Thu Sep 29, 2005 12:30 pm Post subject: Re: How to measure code-efficiency? |
|
|
| Quote: | #include
// ...
clock_t t0 = clock();
// ---- insert code to be timed ----
double tElapsedSec = clock() - t0;
tElapsedSec /= CLOCKS_PER_SEC;
// ...
|
However, if the time elapsed is really low it shows 0. Is there any way
to measure thousandths of a second?
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Thu Sep 29, 2005 2:52 pm Post subject: Re: How to measure code-efficiency? |
|
|
"Gaijinco" <gaijinco (AT) gmail (DOT) com> wrote
| Quote: | #include
// ...
clock_t t0 = clock();
// ---- insert code to be timed ----
double tElapsedSec = clock() - t0;
tElapsedSec /= CLOCKS_PER_SEC;
// ...
However, if the time elapsed is really low it shows 0. Is there any way
to measure thousandths of a second?
|
There's no standard portable way. The granularity of
the time used by 'clock()' is implementation defined.
However, most compilers for PCs I've used have
millisecond granularity (i.e. CLOCKS_PER_SEC == 1000).
Another possibility is to use a nonstandard extension
provided by your compiler if it has one. Check your
documentation.
-Mike
|
|
| Back to top |
|
 |
Walter Bright Guest
|
Posted: Thu Sep 29, 2005 6:31 pm Post subject: Re: How to measure code-efficiency? |
|
|
"Gaijinco" <gaijinco (AT) gmail (DOT) com> wrote
| Quote: | Every now and then and found two ways to resolve a problem. I try to
find a way to decide which one is best (at least speed-wise) but I
don't know how do I test how long those it take a program to run?
|
This should help:
http://www.digitalmars.com/techtips/timing_code.html
-Walter
www.digitalmars.com C, C++, D programming language compilers
|
|
| Back to top |
|
 |
nyoescape@gmail.com Guest
|
Posted: Thu Sep 29, 2005 6:35 pm Post subject: Re: How to measure code-efficiency? |
|
|
You can do it indirectly, by repeating it again and again for a
specified time:
clock_t t0 = clock();
unsigned long n = 0;
while ( float(clock()-t0)/CLOCKS_PER_SEC < 1) {
// code here will repeat itself for a second
++n;
}
double elapsed = (float(clock()-t0)/CLOCKS_PER_SEC)/n;
That's what I usually do - it gets more accurate the longer you repeat
the code.
|
|
| Back to top |
|
 |
nyoescape@gmail.com Guest
|
Posted: Thu Sep 29, 2005 6:51 pm Post subject: Re: How to measure code-efficiency? |
|
|
Oops, should've use double() instead of float() there. Sorry.
|
|
| Back to top |
|
 |
kamit Guest
|
Posted: Thu Sep 29, 2005 6:57 pm Post subject: Re: How to measure code-efficiency? |
|
|
how about using gettimeofday?
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Thu Sep 29, 2005 7:08 pm Post subject: Re: How to measure code-efficiency? |
|
|
"kamit" <khandelwal.amit (AT) gmail (DOT) com> wrote
| Quote: | how about using gettimeofday?
|
No such function in standard C++.
Also, 'time of day' functions, standard or not,
typically don't have as low a granularity as
'clock()'.
-Mike
|
|
| Back to top |
|
 |
mlimber Guest
|
Posted: Thu Sep 29, 2005 7:41 pm Post subject: Re: How to measure code-efficiency? |
|
|
Gaijinco wrote:
| Quote: | Every now and then and found two ways to resolve a problem. I try to
find a way to decide which one is best (at least speed-wise) but I
don't know how do I test how long those it take a program to run?
|
Several points:
* Generally speaking, given two pieces of code that solve the same
problem, you should keep the one that is the most portable,
maintainable, and extensible. Speed should not be a primary
consideration unless the piece of code is a *known* bottleneck (see the
next two points).
* You can use a profiler to calculate how long a particular piece of
code takes to run, and more importantly, you use a profiler to find out
if that piece of code is a bottleneck. There is no sense in optimizing
(or recoding in assembler or whatever) when a working, unoptimized
piece of code meets the requirements. Check your platform or
development documentation for more information on profiling on your
system.
* Beware Premature Optimization (from
[url]http://gotw.ca/publications/mill09.htm):[/url]
'If you're a regular reader of this column, you'll already be familiar
with my regular harangues against premature optimization. The rules
boil down to: "1. Don't optimize early. 2. Don't optimize until you
know that it's needed. 3. Even then, don't optimize until you know what
needed, and where."
'By and large, programmers--that includes you and me--are notoriously
bad at guessing the actual space/time performance bottlenecks in their
own code. If you don't have performance profiles or other empirical
evidence to guide you, you can easily spend days optimizing something
that doesn't need optimizing and that won't measurably affect runtime
space or time performance. What's even worse, however, is that when you
don't understand what needs optimizing you may actually end up
pessimizing (degrading your program) by of saving a small cost while
unintentionally incurring a large cost. Once you've run performance
profiles and other tests, and you actually know that a particular
optimization will help you in your particular situation, then it's the
right time to optimize.'
Cheers! --M
|
|
| Back to top |
|
 |
Branimir Maksimovic Guest
|
Posted: Thu Sep 29, 2005 9:49 pm Post subject: Re: How to measure code-efficiency? |
|
|
David White wrote:
| Quote: | Gaijinco wrote:
Every now and then and found two ways to resolve a problem. I try to
find a way to decide which one is best (at least speed-wise) but I
don't know how do I test how long those it take a program to run?
I time code execution like this:
#include <ctime
// ...
clock_t t0 = clock();
// ---- insert code to be timed ----
double tElapsedSec = clock() - t0;
tElapsedSec /= CLOCKS_PER_SEC;
// ...
It isn't that simple. clock can wrap around in some time. |
clock_t clockdiff(clock_t s,clock_t e)
{
assert(std::numeric_limits
assert(s != clock_t(-1) && e != clock_t(-1) && s>=0 && e>=0);
if(s<=e)return e-s;
else return (std::numeric_limits
}
Greetings, Bane.
|
|
| Back to top |
|
 |
ben Guest
|
Posted: Sun Oct 09, 2005 10:10 am Post subject: Re: How to measure code-efficiency? |
|
|
Gaijinco wrote:
| Quote: | Every now and then and found two ways to resolve a problem. I try to
find a way to decide which one is best (at least speed-wise) but I
don't know how do I test how long those it take a program to run?
|
Just run each version serveral times in different system loadings and if
you still can't feel the speed difference then they are pretty much the
same.
Ben
|
|
| Back to top |
|
 |
Alex Vinokur Guest
|
|
| Back to top |
|
 |
|