473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

formatting time as milliseconds in logging module

Hi!

The logging module nicely prepends each line with a formatted date.

However, I'm not interested in the actual date but only in the number of
milliseconds that passed since the start of the program. What is the best
way of doing that?

Thanks,
Stefan
Jul 18 '05 #1
6 4620
> The logging module nicely prepends each line with a formatted date.

However, I'm not interested in the actual date but only in the number of
milliseconds that passed since the start of the program. What is the best
way of doing that?


In Windows: time.clock() will get you the number of seconds since you
last called it, down to the microsecond (trim as you see fit).

In linux, time.clock() gives you the amount of processor time used, so
it is not quite as useful in this case.
In both, time.time() will give you the number of seconds since the epoch,
so you can call it once when the program starts, and later just
subtract the times. In Windows, it is only accurate to .01 seconds, and
I don't know how accurate the linux version is (I expect about as
accurate, but I can certainly be wrong).

- Josiah

Jul 18 '05 #2
On Tue, 19 Oct 2004 09:47:30 -0700, Josiah Carlson <jc******@uci.edu> wrote:
The logging module nicely prepends each line with a formatted date.

However, I'm not interested in the actual date but only in the number of
milliseconds that passed since the start of the program. What is the best
way of doing that?
In Windows: time.clock() will get you the number of seconds since you
last called it, down to the microsecond (trim as you see fit).

^^^^ first, it seems (certainly not a delta since the last time.clock() call)
if 1: #postpone interactive start of suite

... print 'starting'
... import time
... time.sleep(5)
... print time.clock()
... for i in xrange(10):
... print time.clock()
... time.sleep(1.0)
...
starting
1.25714266558e-005
0.00067466656386
1.00140967598
2.00285203766
3.00429188506
4.00575352293
5.00716152272
6.0086038844
7.01003954133
8.01151542682
9.01291420756

Don't know why the first value is not a pure 0.0 though.

In linux, time.clock() gives you the amount of processor time used, so
it is not quite as useful in this case.
In both, time.time() will give you the number of seconds since the epoch,
so you can call it once when the program starts, and later just
subtract the times. In Windows, it is only accurate to .01 seconds, and Maybe not even all windows. E.g., for NT that is one scheuling time slice,
but oher variants might have other granularity, IWT.
I don't know how accurate the linux version is (I expect about as
accurate, but I can certainly be wrong).

Unless your process is running with hard real time priority, you have to throw in
os multitasking and interrupt processing latency as well (try using the mouse to drag
a complex GUI window of another app around continuously while your test runs ;-).

Regards,
Bengt Richter
Jul 18 '05 #3
In Windows: time.clock() will get you the number of seconds since you
last called it, down to the microsecond (trim as you see fit). ^^^^ first, it seems (certainly not a delta since the last time.clock() call)


Oops, word slip. My bad, I meant first.
Don't know why the first value is not a pure 0.0 though.
The C source tells us that in Windows, the performance counter call to
guarantee that it has been started occurs before the counter call to
actually get the number of seconds since the first call.

Maybe not even all windows. E.g., for NT that is one scheuling time slice,
but oher variants might have other granularity, IWT.
Good point, though I think that 9x variants also had .01 second
resolution.

Unless your process is running with hard real time priority, you have to throw in
os multitasking and interrupt processing latency as well (try using the mouse to drag
a complex GUI window of another app around continuously while your test runs ;-).


Now you are talking about external system loads and how they effect
timings. That wasn't the question.

- Josiah

Jul 18 '05 #4
On Tue, 19 Oct 2004 11:24:15 -0700, Josiah Carlson <jc******@uci.edu> wrote:
>In Windows: time.clock() will get you the number of seconds since you
>last called it, down to the microsecond (trim as you see fit). ^^^^ first, it seems (certainly not a delta since the last time.clock() call)


Oops, word slip. My bad, I meant first.
Don't know why the first value is not a pure 0.0 though.


The C source tells us that in Windows, the performance counter call to
guarantee that it has been started occurs before the counter call to
actually get the number of seconds since the first call.

Yes I suspected as much. You are presumably referring to
-----------------
if (divisor == 0.0) {
LARGE_INTEGER freq;
QueryPerformanceCounter(&ctrStart);
if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) {
/* Unlikely to happen - this works on all intel
machines at least! Revert to clock() */
return PyFloat_FromDouble(clock());
}
divisor = (double)freq.QuadPart;
}
QueryPerformanceCounter(&now);
diff = (double)(now.QuadPart - ctrStart.QuadPart);
return PyFloat_FromDouble(diff / divisor);
-----------------
which IMO really should be (untested):
-----------------
if (divisor == 0.0) {
LARGE_INTEGER freq;
QueryPerformanceCounter(&ctrStart);
if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) {
/* Unlikely to happen - this works on all intel
machines at least! Revert to clock() */
return PyFloat_FromDouble(clock());
}
divisor = (double)freq.QuadPart;
now = ctrStart;
} else {
QueryPerformanceCounter(&now);
}
diff = (double)(now.QuadPart - ctrStart.QuadPart);
return PyFloat_FromDouble(diff / divisor);
-----------------
Maybe not even all windows. E.g., for NT that is one scheuling time slice,
but oher variants might have other granularity, IWT.
Good point, though I think that 9x variants also had .01 second
resolution.

I don't recall, but the old ~55ms tick was used a lot in the old days.
Unless your process is running with hard real time priority, you have to throw in
os multitasking and interrupt processing latency as well (try using the mouse to drag
a complex GUI window of another app around continuously while your test runs ;-).


Now you are talking about external system loads and how they effect
timings. That wasn't the question.

True.

Regards,
Bengt Richter
Jul 18 '05 #5

bo**@oz.net (Bengt Richter) wrote:
Yes I suspected as much. You are presumably referring to
[snip original C source]
Yes.

which IMO really should be (untested):
----------------- [snip unchanged code] divisor = (double)freq.QuadPart;
now = ctrStart;
} else {
QueryPerformanceCounter(&now);
}
diff = (double)(now.QuadPart - ctrStart.QuadPart);
return PyFloat_FromDouble(diff / divisor);
-----------------


That would be arbitrarily more accurate, but on my machine it is a
difference of around 10^-5 seconds. I don't think it really makes a
difference.

Feel free to submit a patch.

In the context of the original question, the poster would likely be
rounding to the nearest 1/1000 second, so the tens of microseconds
doesn't seem to be a concern.

Maybe not even all windows. E.g., for NT that is one scheuling time slice,
but oher variants might have other granularity, IWT.


Good point, though I think that 9x variants also had .01 second
resolution.

I don't recall, but the old ~55ms tick was used a lot in the old days.


According to a few sources, windows 98 time slices were around 20ms.
Whether or not you could get time resolution beyond the time slice with
time.time(), let us find out. I just happen to have a P166 running
windows 98...

Hrm, looks like 18 or 19 unique times each second, which is around the
55ms tick time you offer. Ick.

- Josiah

Jul 18 '05 #6
Stefan Behnel <be*******@dvs1.informatik.tu-darmstadt.de> wrote in message news:<cl**********@lnx107.hrz.tu-darmstadt.de>...
Hi!

The logging module nicely prepends each line with a formatted date.

However, I'm not interested in the actual date but only in the number of
milliseconds that passed since the start of the program. What is the best
way of doing that?

Thanks,
Stefan


Use the relativeCreated attribute of the LogRecord in your message format. See

http://www.red-dove.com/logging/publ...ter-class.html

Regards,
Vinay Sajip
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: rh0dium | last post by:
Hi all, So I have a slice of code which calls other python code. I have started to take a real liking to the logging module, but I want to extend this into the called python code. I have no...
23
by: Rotem | last post by:
Hi, while working on something in my current project I have made several improvements to the logging package in Python, two of them are worth mentioning: 1. addition of a logging record field...
18
by: Max | last post by:
This is a follow-up on my previous thread concerning having the program wait for a certain date and time and then executing some code when it gets there. My question is; can I use the Sleep...
2
by: Harlin Seritt | last post by:
How can I take a time given in milliseconds (I am doing this for an uptime script) and convert it to human-friendly time i.e. "4 days, 2 hours, 25 minutes, 10 seonds."? Is there a function from the...
3
by: cj | last post by:
If I want to check to see if it's after "11:36 pm" what would I write? I'm sure it's easy but I'm getting tired of having to work with dates and times. Sometimes I just want time or date. And...
3
by: Gary | last post by:
f.CreationTime.Hour.ToString() + ":" + f.CreationTime.Minute.ToString() + ":" + f.CreationTime.Millisecond.ToString(); Which produces a format like 9:30:124 Now I'd like to change this...
4
by: Deniz Dogan | last post by:
Hello. I need help with a small problem I'm having. I want to make a function which takes an integer representing some time in milliseconds and returns the same time but formatted as...
1
by: Peri | last post by:
Dear All, I am developing 2 applications. The first application will keep on generate a new file in a span of 30 milliseconds with some valid data inside (This code is written in C). The...
3
by: Lowell Alleman | last post by:
Here is the situation: I wrote my own log handler class (derived from logging.Handler) and I want to be able to use it from a logging config file, that is, a config file loaded with the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.