C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to measure code-efficiency?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Gaijinco
Guest





PostPosted: Thu Sep 29, 2005 3:18 am    Post subject: How to measure code-efficiency? Reply with 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?

Back to top
puzzlecracker
Guest





PostPosted: Thu Sep 29, 2005 3:32 am    Post subject: Re: How to measure code-efficiency? Reply with quote




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





PostPosted: Thu Sep 29, 2005 4:23 am    Post subject: Re: How to measure code-efficiency? Reply with quote



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





PostPosted: Thu Sep 29, 2005 12:30 pm    Post subject: Re: How to measure code-efficiency? Reply with quote

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





PostPosted: Thu Sep 29, 2005 2:52 pm    Post subject: Re: How to measure code-efficiency? Reply with quote


"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





PostPosted: Thu Sep 29, 2005 6:31 pm    Post subject: Re: How to measure code-efficiency? Reply with quote


"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





PostPosted: Thu Sep 29, 2005 6:35 pm    Post subject: Re: How to measure code-efficiency? Reply with quote

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





PostPosted: Thu Sep 29, 2005 6:51 pm    Post subject: Re: How to measure code-efficiency? Reply with quote

Oops, should've use double() instead of float() there. Sorry.

Back to top
kamit
Guest





PostPosted: Thu Sep 29, 2005 6:57 pm    Post subject: Re: How to measure code-efficiency? Reply with quote

how about using gettimeofday?

Back to top
Mike Wahler
Guest





PostPosted: Thu Sep 29, 2005 7:08 pm    Post subject: Re: How to measure code-efficiency? Reply with quote


"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





PostPosted: Thu Sep 29, 2005 7:41 pm    Post subject: Re: How to measure code-efficiency? Reply with quote

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





PostPosted: Thu Sep 29, 2005 9:49 pm    Post subject: Re: How to measure code-efficiency? Reply with quote

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





PostPosted: Sun Oct 09, 2005 10:10 am    Post subject: Re: How to measure code-efficiency? Reply with quote

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





PostPosted: Tue Oct 25, 2005 6:13 pm    Post subject: Re: How to measure code-efficiency? Reply with quote


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?

To try to use C/C++ Program Perfometer
http://lists.sourceforge.net/lists/listinfo/cpp-perfometer-users

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.