473,804 Members | 3,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 17116

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.goo glegroups.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.goo glegroups.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

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

Similar topics

3
330
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
15932
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 hooked up to interfaces. The Gtk is more than enough gui.
13
1620
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 driver to test the code. I now have 2 remaining problems: 1. The code will allow for the input of distances in either cm, m or km values; however, it isn't smart enough to convert values of different types (ie cm and km) for correct follow on...
6
61834
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> #include <assert.h> #include <time.h> int main() { clock_t start, stop;
5
1408
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 of kb in the Properties of the file. Then I delete everything but the viewstate and measue it the same way. 1. Is it accurate? 2. Is there a better way? (I want to measure over 100 pages.) --
13
10588
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 less than 500 milliseconds.), I also tried the DateTime and it also gives me minimum resolution of 500 milliseconds. But I need to measure much less the 500 milliseconds, I need it to be several milliseconds. How can I do that? --------
8
46807
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 library called ctime. But I dont know what functions to use. Please tell me.
4
12865
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 download speed. the idea is this. 1. i will put a file at a serverA. 2. i download the file 3. i measure the speed of the download(how long it would take me to complete the download)
1
2758
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 functions...and i need to measure the time it encrypts the data consecutively for all the 4 functions till end of file... I am fwding you the codes...Please help me out... int Encrypt (word8 a, word8 rk) { // Encryption of one blockl int r; sec_init();...
3
1954
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 http://www.hazelwood.k12.mo.us/~grichert/sciweb/phys8.htm): When measuring, there is some uncertainty as to the *exact* value; so, when doing calculations with these measured numbers, one should not present an answer that seems more accurate than the source. For...
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10580
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9157
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7621
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6854
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2993
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.