Portál AbcLinuxu, 25. října 2025 01:24
#include < iostream >
#include < time.h >
main()
{
clock_t st1 = clock();
sleep(2);
clock_t st2 = clock();
double dur = (double)(st2-st1)/CLOCKS_PER_SEC;
std::cout<< st1 << std::endl;
std::cout<< st2 << std::endl;
std::cout<< dur << std::endl;
};
...výstupem tohoto programu jsou jen nuly. Dokonce i na internetu jsem pak našel takovýto program. Proč mi nevrací čas ? Je snad na tom jednom příkazu něco blbě ? Jak určit přesný čas ? Za případnou radu předem díky ...
gettimeofday().
.
#include <sys/time.h>V manuálových stránkách samozřejmě je -- v něčem jako man-pages nebo manpages-dev, kde jsou i ostatní manuálové stránky funkcí libc a systémových volání.
GETTIMEOFDAY(2) Linux Programmer’s Manual GETTIMEOFDAY(2)
NAME
gettimeofday, settimeofday - get / set time
SYNOPSIS
#include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv , const struct timezone *tz);
man gettimeofday pravi toto:
GETTIMEOFDAY(2) Linux Programmer's Manual GETTIMEOFDAY(2)
NAME
gettimeofday, settimeofday - get / set time
SYNOPSIS
#include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv , const struct
timezone *tz);
Takze si myslim, ze je to jasne.
#include <stdio.h>
#include <time.h>
void delay_SS(double milisekund)
{
clock_t start, end;
start = clock();
end = start + (clock_t) ((milisekund * (double) CLOCKS_PER_SEC) / (double) 1000);
while ( start < end ) start = clock();
}
int main(void)
{
clock_t start, end;
unsigned int i;
start = clock();
delay_SS(2000);
end = clock();
printf("\nProgram trval: %6.2f sec\n", (end - start) / (double) CLOCKS_PER_SEC);
return 0;
}
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.