Re: [Tutor] python time
Only just spotted this. "spir" wrote It's not C's function, it's a Unix system call. It's been part of Unix since BSD 4.2 I am confused here. That's what I first thought (there _must_ be a way to get time more precise that seconds!). But on my system (ubuntu 9.10) I cannot find the proper manner to use these system calls. Even from the command-line directly. You cannot call system calls from the command line. You can only execute Unixc commands from the shell. A command is a standaline program(executable), it is quite different to a system call. A system call in Unix is the C API provided by the kernel. It can only be called by C or some other language that can link with the kernel libraries - eg Fortran, Assembler etc. So when you use Python it calls the Unix system call via C. [Aside: There is an excellent O'Reilly book on using the Unix system calls from C: "Unix Systems Programming for SVR4" Much of it is directly applicable to Python - sockets, file handling, time, fork, spawn, etc It can be bought used for just a few dollars on Amazon...] This let me think gettimeofday() and ftime() are C routines, They are C functions provided by the Unix kernel. This makes them Unix OS system calls rather than C standard library functions. time() is the lowest common denominator supported by any ANSI C system. Whereas time is provided by the C standard libvrary and is not part of the Unix kernel. So, I still have the issue of not beeing smart enough to access one of these systems-provided features. You access them by using Python. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python time
>> Doesn't time.time return a float? >> >> >>> import time >> >>> help(time.time) >> Help on built-in function time in module time: >> >> time(...) >> time() -> floating point number >> >> Return the current time in seconds since the Epoch. >> Fractions of a second may be present if the system clock provides >> them. >> >> >> >>> time.time() >> 1259288538.576565 >> >> Right? >> -Modulok- > > For sure, but this wasn't my question. I was precisely asking how python > does that. Clearly I have a mighty need to consume more coffee. My apologies for misreading the original post. -Modulok- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python time
On Fri, Nov 27, 2009 at 6:07 PM, spir wrote: > I am confused here. That's what I first thought (there _must_ be a way to get > time more precise that seconds!). But on my system (ubuntu 9.10) I cannot > find the proper manner to use these system calls. Even from the command-line > directly. Certainly missing something _really_ obvious. I think you have to call them from C or some other language that can access the system calls. >> So only seconds need to be supported for ANSI compliance. > > Right. This is precisely the reason why some languages (eg lua) don't provide > any way to get time in ms or us. And also the source of my original post on > the topic. Some solutions for Lua here: http://stackoverflow.com/questions/463101/lua-current-time-in-milliseconds Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python time
"Alan Gauld" wrote: Thank you, Alan. > "spir" wrote > > > So, python uses C's gettimeofday() func when available > > It's not C's function, it's a Unix system call. > It's been part of Unix since BSD 4.2 > > > ftime() (millisecond), > > Also Unix and predates BSD 4.2... I am confused here. That's what I first thought (there _must_ be a way to get time more precise that seconds!). But on my system (ubuntu 9.10) I cannot find the proper manner to use these system calls. Even from the command-line directly. Certainly missing something _really_ obvious. As for the code in timemodule.c, I read: floattime(void) { /* There are three ways to get the time: (1) gettimeofday() -- resolution in microseconds (2) ftime() -- resolution in milliseconds (3) time() -- resolution in seconds In all cases the return value is a float in seconds. Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may fail, so we fall back on ftime() or time(). Note: clock resolution does not imply clock accuracy! */ #ifdef HAVE_GETTIMEOFDAY { struct timeval t; #ifdef GETTIMEOFDAY_NO_TZ if (gettimeofday(&t) == 0) return (double)t.tv_sec + t.tv_usec*0.01; #else /* !GETTIMEOFDAY_NO_TZ */ if (gettimeofday(&t, (struct timezone *)NULL) == 0) return (double)t.tv_sec + t.tv_usec*0.01; #endif /* !GETTIMEOFDAY_NO_TZ */ } #endif /* !HAVE_GETTIMEOFDAY */ ... This let me think gettimeofday() and ftime() are C routines, but they may ne wrappers around system calls. > > > else it has only plain second precision using time(). > > Which is an ANSI C function. > > > But there is no matching system call AFAIK except for time > > (via datetime in unix-like systems). > > time() is the lowest common denominator supported by any ANSI C system. > It returns a tm struct: > > struct tm{ > int tm_sec; > int tm_min; > // etc up to > int tm_yday; > int tm_isdst; > } > > So only seconds need to be supported for ANSI compliance. Right. This is precisely the reason why some languages (eg lua) don't provide any way to get time in ms or us. And also the source of my original post on the topic. > gettimeofday() is the Unix system call that returns microseconds. > > But to confuse things, on Unix there is also the time() library function > (not system call - subtle difference!) which returns a timeval structure: > > struct timeval{ > long tv_sec; > long tv_usec; > } > > So even time() on Unix returns microsecs. > > But on early DOS it only returns seconds. > (I may be wrong but I think time() on DOS 6 returned milliseconds?) > > HTH, So, I still have the issue of not beeing smart enough to access one of these systems-provided features. Would someone be nice & write an example call to use eg Unix's gettimeofday directly from python or from the command-line? (without using python's time module, indeed) Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python time
"spir" wrote So, python uses C's gettimeofday() func when available It's not C's function, it's a Unix system call. It's been part of Unix since BSD 4.2 ftime() (millisecond), Also Unix and predates BSD 4.2... else it has only plain second precision using time(). Which is an ANSI C function. But there is no matching system call AFAIK except for time (via datetime in unix-like systems). time() is the lowest common denominator supported by any ANSI C system. It returns a tm struct: struct tm{ int tm_sec; int tm_min; // etc up to int tm_yday; int tm_isdst; } So only seconds need to be supported for ANSI compliance. gettimeofday() is the Unix system call that returns microseconds. But to confuse things, on Unix there is also the time() library function (not system call - subtle difference!) which returns a timeval structure: struct timeval{ long tv_sec; long tv_usec; } So even time() on Unix returns microsecs. But on early DOS it only returns seconds. (I may be wrong but I think time() on DOS 6 returned milliseconds?) HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python time
Kent Johnson wrote: > On Wed, Nov 25, 2009 at 11:11 AM, spir wrote: > > Hello, > > > > How does python get the time in microseconds? (In other words, how would I > > get it if python (like some other languages) would provide time in whole > > seconds only?) > > Use the source...in particular, see floattime() in timemodule.c: > http://svn.python.org/view/python/branches/release31-maint/Modules/timemodule.c?view=markup > > Kent > Thanks, Kent. So, python uses C's gettimeofday() func when available (microsecond), or ftime() (millisecond), else it has only plain second precision using time(). But there is no matching system call AFAIK except for time (via datetime in unix-like systems). So, does someone know of any solution via the system itself? How do C funcs get precise time, anyway? Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python time
On Thu, Nov 26, 2009 at 9:28 PM, Modulok wrote: > Doesn't time.time return a float? time.time() > 1259288538.576565 > > Right? Yes. I'm not at all sure I understand the original question, but one plausible interpretation is, "How does python get the time to high accuracy? I want to do that in my programs." That is the question I was answering. Kent > -Modulok- > > On 11/26/09, Kent Johnson wrote: >> On Wed, Nov 25, 2009 at 11:11 AM, spir wrote: >>> Hello, >>> >>> How does python get the time in microseconds? (In other words, how would I >>> get it if python (like some other languages) would provide time in whole >>> seconds only?) >> >> Use the source...in particular, see floattime() in timemodule.c: >> http://svn.python.org/view/python/branches/release31-maint/Modules/timemodule.c?view=markup >> >> Kent >> ___ >> Tutor maillist - tu...@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python time
Doesn't time.time return a float? >>> import time >>> help(time.time) Help on built-in function time in module time: time(...) time() -> floating point number Return the current time in seconds since the Epoch. Fractions of a second may be present if the system clock provides them. >>> time.time() 1259288538.576565 Right? -Modulok- On 11/26/09, Kent Johnson wrote: > On Wed, Nov 25, 2009 at 11:11 AM, spir wrote: >> Hello, >> >> How does python get the time in microseconds? (In other words, how would I >> get it if python (like some other languages) would provide time in whole >> seconds only?) > > Use the source...in particular, see floattime() in timemodule.c: > http://svn.python.org/view/python/branches/release31-maint/Modules/timemodule.c?view=markup > > Kent > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python time
On Wed, Nov 25, 2009 at 11:11 AM, spir wrote: > Hello, > > How does python get the time in microseconds? (In other words, how would I > get it if python (like some other languages) would provide time in whole > seconds only?) Use the source...in particular, see floattime() in timemodule.c: http://svn.python.org/view/python/branches/release31-maint/Modules/timemodule.c?view=markup Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python time
spir wrote: Hello, How does python get the time in microseconds? (In other words, how would I get it if python (like some other languages) would provide time in whole seconds only?) Thank you, Denis la vita e estrany http://spir.wikidot.com/ You need to supply more information about your environment, and maybe the reason for asking. Since you don't, I'll make some guesses. If you're trying to do extremely fine delta-time measurements, there is a Pentium instruction to fetch the number of clock cycles. You'd get this in assembler language, or with _asm in C code. If you're on Windows, there is a NtQuerySystemTime() function exported by ntdll.dll. It fetches a large integer specifying the number of 100-ns intervals since some epoch time in 1601. It's been deprecated, however, in favor of GetSystemTimeAsFileTime(), available in Win2k and later, and exported from kernel32.dll There are others, but I can't find them at the moment. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python time
spir wrote: Hello, How does python get the time in microseconds? time.time() should provide fractional second accuracy if the system provides them. Did the system clock actually record fractional seconds? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python time
"spir" wrote How does python get the time in microseconds? The underlying OS API generally provides that. (In other words, how would I get it if python (like some other languages) would provide time in whole seconds only?) You would have to call the OS routines directly from Python yourself. Alan G ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] python time
Hello, How does python get the time in microseconds? (In other words, how would I get it if python (like some other languages) would provide time in whole seconds only?) Thank you, Denis la vita e estrany http://spir.wikidot.com/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor