Files
SaraP 739088af9f Vroni 7.8 :
- aggiornamento versione.
2025-01-29 16:24:30 +01:00

208 lines
7.7 KiB
C++

/*****************************************************************************/
/* */
/* Copyright (C) 1993-2025 M. Held */
/* */
/* This code is not in the public domain. All rights reserved! Please make */
/* sure to read the full copyright statement contained in "README.txt" or in */
/* the "main" file of this code, such as "main.cc". */
/* */
/*****************************************************************************/
/* */
/* Written by: Martin Held */
/* E-Mail: held@cs.sbg.ac.at */
/* Fax Mail: (+43 662) 8044-611 */
/* Voice Mail: (+43 662) 8044-6304 */
/* Snail Mail: Martin Held */
/* FB Informatik */
/* Universitaet Salzburg */
/* A-5020 Salzburg, Austria */
/* */
/*****************************************************************************/
#include "vroni_object.h"
#ifdef NO_CPUTIME
double vroniObject::elapsed()
{
return 0.0;
}
#else /* ifndef NO_CPUTIME */
#include <stdio.h>
/* */
/* Note: CPUTIME_IN_MILLISECONDS can be defined in the Imakefile by using */
/* flag "-DCPUTIME_IN_MILLISECONDS". Same for the other flags. */
/* */
#ifdef CPUTIME_IN_MILLISECONDS
#include <sys/resource.h>
#ifndef RUSAGE_CHILDREN
#undef CPUTIME_IN_MILLISECONDS
#endif
#endif
#ifdef CPUTIME_BY_CHRONO
#include <chrono>
/* */
/* This procedure computes the CPU time used by the calling process since */
/* the last call. It is based on the chrono library of C++. */
/* Call it before and after the execution of some instructions of your */
/* program, whose CPU consumption you would like to know. The first value */
/* returned can be ignored, and the second value gives the actual CPU */
/* consumption in the form milliseconds.microseconds. (I.e., 12345 microsecs */
/* are returned as 12.345 millisecs.) */
/* */
double vroniObject::elapsed()
{
double cpu_time;
if (!elapsed_initialized) {
elapsed_initialized = true;
start = std::chrono::high_resolution_clock::now();
cpu_time = 0.0;
}
else {
auto end = std::chrono::high_resolution_clock::now();
auto diff = end - start;
cpu_time = std::chrono::duration<double, std::micro>(diff).count() / 1000.0;
start = end;
}
return cpu_time;
}
#else
#ifdef CPUTIME_IN_MILLISECONDS
/* */
/* This procedure computes the CPU time used by the calling process since */
/* the last call. (The sum of both system and user time is reported.) */
/* Call it before and after the execution of some instructions of your */
/* program, whose CPU consumption you would like to know. The first value */
/* returned can be ignored, and the second value gives the actual CPU */
/* consumption in the form milliseconds.microseconds. (I.e., 12345 microsecs */
/* are returned as 12.345 millisecs.) */
/* */
double vroniObject::elapsed()
{
double cpu_time;
unsigned long new_secs, secs, new_usecs, usecs, new_add_secs;
int getrusage(int who, struct rusage *rusage);
struct rusage rusage_self, rusage_children;
getrusage(RUSAGE_SELF, &rusage_self);
getrusage(RUSAGE_CHILDREN, &rusage_children);
new_secs = rusage_self.ru_utime.tv_sec +
rusage_self.ru_stime.tv_sec +
rusage_children.ru_utime.tv_sec +
rusage_children.ru_stime.tv_sec;
new_usecs = rusage_self.ru_utime.tv_usec +
rusage_self.ru_stime.tv_usec +
rusage_children.ru_utime.tv_usec +
rusage_children.ru_stime.tv_usec;
if (new_usecs >= 1000000) {
new_add_secs = new_usecs / 1000000;
new_usecs -= new_add_secs * 1000000;
new_secs += new_add_secs;
}
if (total_usecs > new_usecs) {
secs = new_secs - total_secs - 1;
usecs = new_usecs - total_usecs + 1000000;
}
else {
secs = new_secs - total_secs;
usecs = new_usecs - total_usecs;
}
total_secs = new_secs;
total_usecs = new_usecs;
cpu_time = 1.0e3 * (double) secs + 1.0e-3 * (double) usecs;
return cpu_time;
}
#else /* ifndef CPUTIME_IN_MILLISECONDS */
#ifdef CPUTIME_VIA_CLOCK
#define CLOCK_UNITS 1000.0 /* this could be figured out automatically */
/* */
/* This procedure computes the CPU time used by the calling process since */
/* the last call. (The sum of both system and user time is reported.) */
/* Call it before and after the execution of some instructions of your */
/* program, whose CPU consumption you would like to know. The first value */
/* returned can be ignored, and the second value gives the actual CPU */
/* consumption in the form milliseconds.microseconds. (I.e., 12345 microsecs */
/* are returned as 12.345 millisecs.) */
/* */
double vroniObject::elapsed()
{
double cpu_time;
clock_t finish;
#ifndef CLOCKS_PER_SEC
#define CLOCKS_PER_SEC sysconf(_SC_CLK_TCK)
#endif
if (!elapsed_initialized) {
total_cpu_time = clock();
elapsed_initialized = true;
}
finish = clock();
cpu_time = ((double) (finish - total_cpu_time)) *
(CLOCK_UNITS / CLOCKS_PER_SEC);
total_cpu_time = finish;
return cpu_time;
}
#else /* ifndef CPUTIME_IN_MILLISECONDS || CPUTIME_VIA_CLOCK */
#include <sys/types.h>
#include <sys/times.h>
#include <unistd.h>
/* */
/* This procedure computes the CPU time used by the calling process since */
/* the last call. (The sum of both system and user time is reported.) */
/* Call it before and after the execution of some instructions of your */
/* program, whose CPU consumption you would like to know. The first value */
/* returned can be ignored, and the second value gives the actual CPU */
/* consumption (in milliseconds). */
/* */
double vroniObject::elapsed()
{
long dummy;
double cpu_time;
struct tms buffer;
if (HZ == 0) HZ = sysconf(_SC_CLK_TCK);
times(&buffer);
dummy = buffer.tms_utime + buffer.tms_stime +
buffer.tms_cutime + buffer.tms_cstime;
cpu_time = (double) ((dummy - total) * 1000.0) / HZ;
total = dummy;
return cpu_time;
}
#endif
#endif
#endif
#endif