LINUX TIPS AND TRICKS --- August 11, 2000

Published by ITworld.com, the IT problem-solving network 
http://www.itworld.com/newsletters 

*********************************************************************

Roll Your Own Time Measuring Class 
by Danny Kalev 

Commercial profiling suites are expensive and require some practice 
before you can use them effectively. This week, I will show how to 
implement a simple and effective stopwatch class that automatically 
measures execution time of functions, loops, and code blocks. 

In C++, the constructor and destructor of an automatic object executes 
at block's beginning and end, respectively. We take advantage of this 
feature: stopwatch's constructor starts counting time and its destructor 
reports the time elapsed. We will use the ANSI function clock() 
(declared in time.h>). clock() returns the processor's time elapsed 
since the program's outset in clock ticks. The macro CLOCKS_PER_SEC 
represents the number of clock ticks per second on your machine. 

Our stopwatch class looks like this: 

    #include time.h> 

    class stopwatch 
    { 
    public: 
     stopwatch() : start(clock()){} // start counting time 
     ~stopwatch(); 
    private: 
     clock_t start; 
    }; 

The constructor initializes the member 'start' with the current tick 
count. The destructor calls clock() again, computes the time elapsed 
since the object's construction and displays the results: 

    stopwatch::~stopwatch() 
    { 
     clock_t total=clock()-start; // get elapsed time 
     cout  "ticks for this activity: "  total  
    '\n'; 
     cout  "in seconds: "   double(total)/CLOCKS_PER_SEC 
     endl; 
    } 

To measure the duration of a code block, create a local stopwatch 
instance at block's beginning. Suppose you want to measure the duration 
of the following loop that allocates 5000 strings dynamically: 

    string *pstr[5000]; // array of pointers 
    for (int i=0;i5000;i++) 
    { 
     pstr[i] = new string; 
    } 

Surround the relevant code in braces and instantiate a stopwatch object 
at block's beginning: 

    { 
     stopwatch watch; // start measuring 
     string *pstr[5000]; 
     for (int i=0;i5000;i++) 
     { 
      pstr[i] = new string; 
     } 
    } // display results 

That's all! When the block begins, watch starts counting time; when it 
exits, watch's destructor displays the results: 

    ticks for this activity: 27 
    in seconds: 0.027 

What is the performance gain of replacing dynamic allocation with stack 
allocation? Let's try it: 

    { 
     stopwatch watch; 
     for (int i=0;i5000;i++) 
     { 
      string s; // local automatic string 
     } 
    } 

This time, the results are as follows: 

    total of clock ticks for this activity: 14 
    in seconds: 0.014 

Using the stopwatch class, we achieved a 50% speed increase by using 
stack memory instead of heap memory. Considering that our heap version 
didn't count the time needed for destroying the 5000 strings -- as 
opposed to the stack version -- the results are even more impressive. 

Resources 

Java Tip 92: Use the JVM Profiler Interface for accurate timing 
Improve performance analysis by measuring Java thread CPU time. 
http://www.javaworld.com/javaworld/javatips/jw-javatip92.html 

Performance perplexities: Help! Where do I start? 
Got a problem? Here are 12 simple questions to ask yourself before you 
ask others. 
http://www.sunworld.com/swol-11-1997/swol-11-perf.html 

Viewing your network in realtime 
An examination of network monitoring protocols and tools. 
http://www.sunworld.com/sunworldonline/swol-09-1999/swol-09-realtime2.html 

************************************************************************ 

About the author 
---------------- 
Danny Kalev is a system analyst and software engineer with more 
than 10 years of experience, specializing in C++ and 
object-oriented analysis and design on various platforms including 
VMS, DOS, Windows, Unix, and Linux. His technical interests involve 
code optimization, networking, and distributed computing. He is 
also a member of the ANSI C++ standardization committee and the 
author of ANSI/ISO C++ Professional Programmer's Handbook (Que, 
1999). Contact him at linuxnl@excite.com. 

*********************************************************************

CUSTOMER SERVICE 

You can subscribe or unsubscribe to any of your e-mail newsletters by 
updating your form at: 
http://www.itworld.com/cgi-bin/w3-msql/newsletters/subcontent12.html? 

For subscription changes that cannot be handled via the web, please send 
an email to our customer service dept: support@itworld.com 

*********************************************************************

CONTACTS 

* For editorial comments, write Andrew Santosusso, Associate Editor, 
Newsletters at: andrew_santosusso@itworld.com 
* For advertising information, write Dan Chupka, Account Executive at: 
dan_chupka@itworld.com 
* For recruitment advertising information, write Jamie Swartz, Eastern Regional Sales Manager at: jamie_swartz@itworld.com or Paul Duthie, Western Regional Sales Manager at: paul_duthie@itworld.com
* For all other inquiries, write Jodie Naze, Product Manager, 
Newsletters at: jodie_naze@itworld.com 

********************************************************************* 

Copyright 2000 ITworld.com, Inc., All Rights Reserved. 

http://www.itworld.com 
