|
Forum Flash, Actionscript, PHP e MySQL
|
|
|
|
|
|
| Autore |
Messaggio |
DrSpeed
tenca


Età: -1989
Registrato: 06/07/06 20:50
Messaggi: 137
Località: Monselice(PD)
|
Oggetto: [c]creazione timer
Inviato: 11.12.06 | 21:07 |
|
|
e possibile creare un timer che misuri in quanto tempo viene eseguita una applicazione? ____________________________________________________________ SNAP_DrSpeed |
|
| Torna in cima |
|
|
Sponsor
|
|
 |
giammy
moderatore


Registrato: 29/04/05 17:22
Messaggi: 75
|
Oggetto: Re: [c]creazione timer
Inviato: 21.12.06 | 15:59 |
|
|
| DrSpeed ha scritto: | | e possibile creare un timer che misuri in quanto tempo viene eseguita una applicazione? |
beh, in linea di principio:
1 - leggi l'ora
2 - lancia il programma e aspetta che esca
3 - leggi l'ora
In linux c'e il programma time:
giammy@pgr12 ~]$ time ls > /dev/null
real 0m0.004s
user 0m0.004s
sys 0m0.000s
ti dice quanto tempo il ocmendo "ls > /dev/null" ha impegato
(tempo effettivo, tempo impiegato dal prg, tempo impegato dal so)
vedi comunque
$ man time
ciao
giammy |
|
| Torna in cima |
|
|
gush
esperto


Registrato: 24/02/03 15:33
Messaggi: 838
Località: Padova
|
Oggetto:
Inviato: 21.12.06 | 18:24 |
|
|
A te:
| Codice: | #ifndef TIMER_H
#define TIMER_H
#include <ctime>
#include <iostream>
#include <iomanip>
class timer
{
friend std::ostream& operator<<(std::ostream& os, timer& t);
private:
bool running;
clock_t start_clock;
time_t start_time;
double acc_time;
double elapsed_time();
public:
// '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) { }
void start(const char* msg = 0);
void restart(const char* msg = 0);
void stop(const char* msg = 0);
void check(const char* msg = 0);
}; // 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 (clock() - start_clock) / (1.0 * CLOCKS_PER_SEC);
else
return (1.0 * 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;
// Set timer status to running and set the start time
running = true;
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 timer status to running, reset accumulated time, and set start time
running = true;
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;
// Compute accumulated running time and set timer status to not running
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(2)
<< 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(2) << std::setiosflags(std::ios::fixed)
<< t.acc_time + (t.running ? t.elapsed_time() : 0);
return os;
}
//===========================================================================
#endif // TIMER_H |
| Codice: | // Example program to demonstrate the timer class
#include "timer.h"
using namespace std;
int main()
{
timer t;
// Start the timer and then report the time required to run a
// big loop. The string argument to start() is optional.
t.start("Timer started");
for (int i = 0; i < 1000000000; ++i);
cout << "Check 1: " << t << endl;
// Restart the timer and time another loop.
t.restart("Timer restarted");
for (int i = 0; i < 1000000000; ++i);
cout << "Check 2: " << t << endl;
// Stop the timer and repeat the loop. The third timer check
// should report the same value as the second.
t.stop("Timer stopped");
for (int i = 0; i < 1000000000; ++i);
cout << "Check 3: " << t << endl;
// Start the timer again. Since there is no restart, the timer
// will start from where it left off the last time it was stopped.
t.start("Timer started, not restarted");
for (int i = 0; i < 1000000000; ++i);
// The 'check' member function gives a default
// method of printing the current elapsed time.
t.check("Check 4");
cout << "Now sleeping for 2 hours...\n";
//sleep(60*60*2);
cout << "Check 5: " << t << endl;
return 0;
} |
 ____________________________________________________________ ◊◊◊ DM-YARD ◊◊◊ |
|
| Torna in cima |
|
|
DrSpeed
tenca


Età: -1989
Registrato: 06/07/06 20:50
Messaggi: 137
Località: Monselice(PD)
|
Oggetto:
Inviato: 22.12.06 | 00:25 |
|
|
grazie!!  ____________________________________________________________ SNAP_DrSpeed |
|
| Torna in cima |
|
|
|
|
Non puoi inserire nuovi Topic in questo forum Non puoi rispondere ai Topic in questo forum Non puoi modificare i tuoi messaggi in questo forum Non puoi cancellare i tuoi messaggi in questo forum Non puoi votare nei sondaggi in questo forum Non puoi allegare files in questo forum Puoi downloadare gli allegati in questo forum
|
|