jos su date utility klase:
*
Timer.h
*
TimerTest.cc
i
*
Generator.h
*
GeneratorTest.cc
--
TIMER.C
Code:
#ifndef TIMER_H
#define TIMER_H
#include <ctime>
#include <iostream>
#include <iomanip>
#define PRECISION 5
class Timer {
bool running;
clock_t start_clock;
time_t start_time;
double acc_time;
public:
friend std::ostream& operator<<(std::ostream& os, Timer& t);
// 'running' is initially false. A timer needs to be explicitly started
// using 'start' or 'restart'
Timer() : running(false), start_clock(0), start_time(0), acc_time(0) { }
double elapsed_time();
void start(const char* msg = 0);
void restart(const char* msg = 0);
void stop(const char* msg = 0);
void check(const char* msg = 0);
char* timeToString(time_t time_stamp, char* time_format);
char* timeToString(time_t time_stamp);
}; // class Timer
//===========================================================================
// Return the total time that the timer has been in the "running"
// state since it was first "started" or last "restarted". For
// "short" time periods (less than an hour), the actual cpu time
// used is reported instead of the elapsed time.
inline double Timer::elapsed_time() {
time_t acc_sec = time(0) - start_time;
if (acc_sec < 3600)
return (double)(clock() - start_clock) / CLOCKS_PER_SEC;
else
return acc_sec;
} // Timer::elapsed_time
//===========================================================================
// Start a timer. If it is already running, let it continue running.
// Print an optional message.
inline void Timer::start(const char* msg) {
// Print an optional message, something like "Starting timer t";
if (msg) std::cout << msg << std::endl;
// Return immediately if the timer is already running
if (running) return;
// Change timer status to running
running = true;
// Set the start time;
start_clock = clock();
start_time = time(0);
} // Timer::start
//===========================================================================
// Turn the timer off and start it again from 0. Print an optional message.
inline void Timer::restart(const char* msg) {
// Print an optional message, something like "Restarting timer t";
if (msg) std::cout << msg << std::endl;
// Set the timer status to running
running = true;
// Set the accumulated time to 0 and the start time to now
acc_time = 0;
start_clock = clock();
start_time = time(0);
} // Timer::restart
//===========================================================================
// Stop the timer and print an optional message.
inline void Timer::stop(const char* msg) {
// Print an optional message, something like "Stopping timer t";
if (msg) std::cout << msg << std::endl;
// Recalculate and store the total accumulated time up until now
if (running) acc_time += elapsed_time();
running = false;
} // Timer::stop
//===========================================================================
// Print out an optional message followed by the current timer timing.
inline void Timer::check(const char* msg) {
// Print an optional message, something like "Checking timer t";
if (msg) std::cout << msg << " : ";
std::cout << "Elapsed time [" << std::setiosflags(std::ios::fixed)
<< std::setprecision(PRECISION)
<< acc_time + (running ? elapsed_time() : 0)
<< "] seconds\n";
} // Timer::check
//===========================================================================
// Allow timers to be printed to ostreams using the syntax 'os << t'
// for an ostream 'os' and a timer 't'. For example, "cout << t" will
// print out the total amount of time 't' has been "running".
inline std::ostream& operator<<(std::ostream& os, Timer& t) {
os << std::setprecision(PRECISION)
<< std::setiosflags(std::ios::fixed)
<< t.acc_time + (t.running ? t.elapsed_time() : 0);
return os;
} // Timer::ostream
//===========================================================================
// represents a timestamp as a string in a specified format
// get actual timestamp with: time_t now = time (0);
// @see http://www.opengroup.org/onlinepubs/007908799/xsh/strftime.html
inline char* timeToString(time_t time_stamp, char* time_format) {
char* time_buffer = new char[40];
const struct tm *tm = localtime ( &time_stamp );
strftime ( time_buffer, 40, time_format, tm );
return time_buffer;
} // timeToString
//===========================================================================
// represents a timestamp as a string in the standardformat given by the
// asctime-function
// @see http://www.cplusplus.com/ref/ctime/
inline char* timeToString(time_t time_stamp) {
struct tm * timeinfo = localtime ( &time_stamp );
return asctime (timeinfo);
} // timeToString
#endif // TIMER_H
GENERATOR.C
Code:
#ifndef GENERATOR_H
#define GENERATOR_H
// siehe http://www.dreamincode.net/forums/showtopic24225.htm
#include <iostream>
#include <ctime>
class Generator {
public:
enum Mode { asc, desc, rand };
private:
unsigned long iCurrent;
Mode mode;
public:
Generator( Mode m = rand, unsigned long iSeed = 0L ) : iCurrent ( iSeed ), mode ( m ) { }
virtual ~Generator( ) { }
virtual void next( unsigned long * puffer, unsigned long size = 1) { //get the next random number
if (mode == asc) {
for (unsigned long i=0; i < size; i++) puffer[i] = ++iCurrent;
} else if (mode == desc) {
for (unsigned long i=0; i < size; i++) puffer[i] = --iCurrent;
} else if (mode == rand) {
for (unsigned long i=0; i < size; i++) {
unsigned long iOutput;
unsigned long iTemp;
//I only want to take the top two bits
//This will shorten our period to (2^32)/16=268,435,456
//Which seems like plenty to me.
for(int r=0; r<16; r++) {
//Since this is mod 2^32 and our data type is 32 bits long
// there is no need for the MOD operator.
iCurrent = (3039177861UL * iCurrent + 1);
iTemp = iCurrent >> 30;
iOutput = iOutput << 2;
iOutput = iOutput + iTemp;
}
puffer[i] = iOutput;
}
}
}
};
#endif // GENERATOR_H
dok se timer.cc i generator.cc mogu skinuti sa ove adrese u zip formatu:
Code:
http://rapidshare.com/files/34...timer_cc_generator_cc.zip.html
ili ovdje cijeli link:
http://rapidshare.com/files/34...tor_cc.zip?killcode=3878429282
[Ovu poruku je menjao SuperC dana 01.06.2007. u 01:18 GMT+1]