/**************************************************************************** ** *FSyTime()...............returntimespentinmilliseconds ** **'SyTime'returnsthenumberofmillisecondsspentbyGAPsofar. ** **Shouldbeasaccurateaspossible,becauseitisusedforprofiling.
*/
UInt SyTime(void)
{ #ifdef EMSCRIPTEN // Emscripten's standard library always returns the same values // for getrusage, which makes some GAP functions enter an // infinite loop, as they use this function to try running // for some period of time. Use NanosecondsSinceEpoch() as // a substitute (it is not perfect, as NanosecondsSinceEpoch() // is walltime, while RUSAGE_SELF is CPU time). return SyNanosecondsSinceEpoch()/1000000000; #else struct rusage buf;
if (getrusage(RUSAGE_SELF, &buf)) {
ErrorMayQuit("SyTime: could not get time: " "%s (errno %d)",
(Int)strerror(errno), (Int)errno);
} return buf.ru_utime.tv_sec * 1000 + buf.ru_utime.tv_usec / 1000; #endif
}
#ifdefined(__APPLE__) && defined(__MACH__) static mach_timebase_info_data_t timeinfo; if (timeinfo.denom == 0) {
(void)mach_timebase_info(&timeinfo);
}
res = timeinfo.numer;
res /= timeinfo.denom; #elifdefined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC) struct timespec ts;
if (clock_getres(CLOCK_MONOTONIC, &ts) == 0) {
res = ts.tv_sec;
res *= 1000000000L;
res += ts.tv_nsec;
} else {
res = 0;
} #elifdefined(HAVE_GETTIMEOFDAY)
res = -1000; #else
res = 0; #endif
res = NEW_PREC(4); // Note this has to be "DYN" since we're not passing a // literal but a const char *
tmp = MakeImmString(method);
AssPRec(res, RNamName("Method"), tmp);
AssPRec(res, RNamName("Monotonic"), monotonic ? True : False);
resolution = SyNanosecondsSinceEpochResolution(); if (resolution > 0) {
AssPRec(res, RNamName("Resolution"), ObjInt_Int8(resolution));
AssPRec(res, RNamName("Reliable"), True);
} elseif (resolution <= 0) {
AssPRec(res, RNamName("Resolution"), ObjInt_Int8(-resolution));
AssPRec(res, RNamName("Reliable"), False);
} return res;
}
/**************************************************************************** ** *FFuncSleep(<self>,<secs>) **
*/ static Obj FuncSleep(Obj self, Obj secs)
{ Int s = GetSmallInt(SELF_NAME, secs);
if (s > 0)
sleep((UInt)s);
// either we used up the time, or we were interrupted. if (HaveInterrupt()) {
ClearError(); // The interrupt may still be pending
ErrorReturnVoid("user interrupt in sleep", 0, 0, "you can 'return;' as if the sleep was finished");
}
return0;
}
/**************************************************************************** ** *FFuncMicroSleep(<self>,<secs>) **
*/ static Obj FuncMicroSleep(Obj self, Obj msecs)
{ Int s = GetSmallInt(SELF_NAME, msecs);
if (s > 0)
usleep((UInt)s);
// either we used up the time, or we were interrupted. if (HaveInterrupt()) {
ClearError(); // The interrupt may still be pending
ErrorReturnVoid( "user interrupt in microsleep", 0, 0, "you can 'return;' as if the microsleep was finished");
}
/**************************************************************************** ** *FInitInfoTime().................tableofinitfunctions
*/ static StructInitInfo module = { // init struct using C99 designated initializers; for a full list of // fields, please refer to the definition of StructInitInfo
.type = MODULE_BUILTIN,
.name = "time",
.initKernel = InitKernel,
.initLibrary = InitLibrary,
};
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.