Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-18-2005, 08:27 AM   #1 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Thumbs up Tip (5 minute solution): The Stopwatch

Students often ask how they can test their algorithms for speed. I'll present you a simple, platform independent class to measure speeds. A function main() is included to demonstrate its usage.

Question: After testing the three differenct loop-functions, why would anyone prefer a certain version?

stopwatch.h
Code:
#ifndef STOPWATCH_H
#define STOPWATCH_H

#include <ctime>

inline static double seconds()
{
  const double secs_per_tick = 1.0 / std::CLOCKS_PER_SEC;
  return ( (double) std::clock() ) * secs_per_tick;
}

class Stopwatch 
{
private:
  bool running_;
  double start_time_;
  double total_;
public:
  inline Stopwatch();
  inline void start();
  inline double stop();
  inline double read();
  inline void resume();
  inline int running();
};

inline Stopwatch::Stopwatch() : 
  running_(0), start_time_(0.0), total_(0.0) 
{}

void Stopwatch::start() 
{
  running_ = true;
  total_ = 0.0;
  start_time_ = seconds();
}

double Stopwatch::stop()  
{
  if (running_)
  {
    total_ += (seconds() - start_time_);
    running_ = 0;
  }
  return total_; 
}

inline void Stopwatch::resume()
{
  if (!running_)
  {
    start_time_ = seconds();
    running_ = 1;
  }
}
    

inline double Stopwatch::read()   
{
  if (running_)
  {
    stop();
    resume();
  }
  return total_;
}

#endif //STOPWATCH_H
main.cpp
Code:
#include "stopwatch.h"
#include <iostream>

using namespace std;

unsigned dTestArray[200000000];

void do_loop()
{
  unsigned i = 0;
  do
  {
    dTestArray[i] = i;
    ++i;   
  } while(i < 200000000);
}  

void while_loop()
{
  unsigned i = 0;
  while(i < 200000000)
  {
    dTestArray[i] = i;
    ++i;
  }  
}

void for_loop()
{
  for(unsigned i = 0; i < 200000000; ++i)
  {
    dTestArray[i] = i;
  }  
}  

int main()
{
  Stopwatch W;
  
  W.start();
  for(unsigned i = 0; i < 20; ++i)
  {
    //Change the loop versions here to compare the loop speeds.
    for_loop();
  }  
  W.stop();
  
  std::cout<<W.read()<<std::endl;
  std::cin.get();
  
  return 0;
}
__________________

Last edited by Valmont; 03-18-2005 at 02:35 PM.
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tip (5 minute solution): The Facade Pattern Valmont Standard C, C++ 3 02-25-2005 12:27 AM
Using Multiple Namespaces within same VC++.NET Solution kash_VCDotNET Platform/API C++ 0 02-19-2005 02:01 AM


All times are GMT -8. The time now is 12:13 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting