473,396 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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?

Sep 29 '05 #1
13 17069

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 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

Sep 29 '05 #2
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;

// ...

DW
Sep 29 '05 #3
> #include <ctime>

// ...

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?

Sep 29 '05 #4

"Gaijinco" <ga******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
#include <ctime>

// ...

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
Sep 29 '05 #5
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.

Sep 29 '05 #6
Oops, should've use double() instead of float() there. Sorry.

Sep 29 '05 #7
how about using gettimeofday?

Sep 29 '05 #8

"kamit" <kh*************@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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
Sep 29 '05 #9
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?


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
http://gotw.ca/publications/mill09.htm):

'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

Sep 29 '05 #10

"Gaijinco" <ga******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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
Sep 29 '05 #11
David White wrote:
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<clock_t>::is_integer);
assert(s != clock_t(-1) && e != clock_t(-1) && s>=0 && e>=0);
if(s<=e)return e-s;
else return (std::numeric_limits<clock_t>::max()-s)+e;
}
Greetings, Bane.

Sep 29 '05 #12
ben
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?


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
Oct 9 '05 #13

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?


To try to use C/C++ Program Perfometer
http://lists.sourceforge.net/lists/l...rfometer-users

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

Oct 25 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gary | last post by:
Hi, How does one set up a timer so I can measure the time to do an event? Something like this I guess the format would be, start timer...
354
by: Montrose... | last post by:
After working in c# for a year, the only conclusion I can come to is that I wish I knew c. All I need is Linux, the gnu c compiler and I can do anything. Web services are just open sockets...
13
by: Chiller | last post by:
I'm now getting close to finishing my Distance class. In the code below I have included a number of overload operators that test for equality etc. I've also added more code in the TEST_DISTANCE...
6
by: Charles M. Reinke | last post by:
I'm using the function clock() to measure the run time of a program so that I can compare among several different algorithms. My code looks like: #include <stdio.h> #include <stdlib.h>...
5
by: Joe Fallon | last post by:
Is there a good way to measure the size of a page delivered to the browser? Also, how do measure the size of Viewstate? I am just using View Source, saving the text file and looking at the number...
13
by: Sharon | last post by:
I need to test a performance of some code of mine. I tried using the Environment.TickCount but its resolution is too low (from the MSDN remarks: The resolution of the TickCount property cannot be...
8
by: saurabh.ss | last post by:
Hi I want to measure the time required to execute a piece of code. For example, I want to know the time required to execute a given function. How do I do it within C++? I know I got to use a...
4
by: halimunan | last post by:
Hello all, I have a question, how do i measure the download/upload speed... as if download rate, upload rate or kbps transfer rate. i want to do my own windows application using C# to measure the...
1
by: kartouss | last post by:
Hello i am new to this forum...can you help me solve this problem.. I have a function encrypt() which encrypts data from a file in 16 bytes till end of file which in turn calls other 4...
3
by: Ethan Furman | last post by:
Hey all. My thanks to all who have responded so far with my other questions. It is much appreciated. Some background on what I'm doing (a good explanation can be found at...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.