Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time(it) magic: Implement minutes/hour formatting and "%%time" cell magic #2855

Merged
merged 7 commits into from Feb 5, 2013
Merged

time(it) magic: Implement minutes/hour formatting and "%%time" cell magic #2855

merged 7 commits into from Feb 5, 2013

Conversation

jankatins
Copy link
Contributor

%%time cell magic and better formatting for longer times (minutes/hours)in both timeit and time magic.

Currently minutes and hours use simple float formatting, not "hours:minutes:seconds" (1.5 min is 1 minute 30 secs).

fixes #2751 (partly)

@minrk
Copy link
Member

minrk commented Jan 26, 2013

Just curious: under what circumstances would you use %timeit -o instead of %time?

@jankatins
Copy link
Contributor Author

I have a network analysis where I put several steps in one cell and then run the whole cell with %%timeit -r1 -n1. I could probably split that or refactor the code into a function, but using cell level timeit was easier. It's also easier to comment out when I don't want to time it anymore.

An example cell (of abut 10 in that notebook, some now commented out):

%%timeit -r1 -n1
eigen = g_giant.eigenvector_centrality()
g_giant.vs["eigen"] = eigen

@jasongrout
Copy link
Member

So would it be easier to just have a %%time cell magic?

@jankatins
Copy link
Contributor Author

If that would do the same as %%timit -r1 -n1: yes...

Oups, now I see why @minrk was asking (one '%'...): yep, for line magics, the %timeit -o is equivalent to %time.

@minrk
Copy link
Member

minrk commented Jan 26, 2013

And yes, %%time should absolutely be registered as a cell magic, I've typed it a million times - that should be a one-liner, if you want to add that here.

@minrk
Copy link
Member

minrk commented Jan 26, 2013

The only advantage left would be the difference in how %time and %timeit report the time (%timeit shows loops, which are irrelevant for one run, but %time doesn't reformat the times if they are significantly less than one second).

@jankatins
Copy link
Contributor Author

Ok, so

  • Add a new internal function which does the time formatting
  • use that in both timeit and time magics
  • Make the time magic cell aware

I can do the first two but the last is something I haven't yet an idea how to do it... lets see...

* Refactor time formatting of timeit into a new function
* Add minutes and hours for long running cells/lines
* Convert time and timeit magic to use the new function

Minutes and hours are float formatted, so 1.5 h is "1 hour, 30 minutes".

Signed-off-by: Jan Schulz <jasc@gmx.net>
Convert time to be usable as cell level magic. Also fix some examples,
which didn't have a '%' in front of the magic.

Signed-off-by: Jan Schulz <jasc@gmx.net>
@jankatins
Copy link
Contributor Author

Hm, that was easier than I thought...

# auto-detecting if the terminal can deal with it, use plain 'us' for
# microseconds. I am really NOT happy about disabling the proper
# 'micro' prefix, but crashing is worse... If anyone knows what the
# right solution for this is, I'm all ears...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're working on this, it would be nice to get the proper µ prefix again - this comment is ancient. I think that checking u'µ'.encode(sys.stdin.encoding) should be OK.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works in a windows command prompt (open cmd, -> ipython)... But in ipythonqt, sys.stdin.encoding is "NoneType"...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about sys.stdout.encoding - that's UTF-8 in the terminal and the Qt console for me. Plus it makes more sense to check it for output. There's some reason we look at stdin elsewhere, but I forget what it is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just read https://bugs.launchpad.net/ipython/+bug/348466 And I'm not sure what happens here: if some user has the terminal encoding set to ascii, it will result in an unicode error.

For example eclipse set the default stdout encoding in the embedded console to None but print works...:

>>> import sys
>>> print(sys.stdout.encoding)
None
>>> print(u'µ'.encode(sys.stdout.encoding))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: encode() argument 1 must be string, not None
>>> print(u'µ')
µ

I think the only "working" solution would be to try to print a "µ" and if hat fails use "u" instead, but I've no idea how to do that without actually printing a char to the console...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

False negatives don't matter much - users in Eclipse will still see 'u'. We
want to avoid false positives, i.e. trying to display µ when we can't do
so. I think using sys.stdout.encoding is an improvement for all the users
where it is set. But if you're not sure, leave it, and we'll do it in a
separate pull request.

On 3 February 2013 12:59, JanSchulz notifications@github.com wrote:

In IPython/core/magics/execution.py:

@@ -1091,3 +1071,43 @@ def parse_breakpoint(text, current_file):
return current_file, int(text)
else:
return text[:colon], int(text[colon+1:])
+
+def _format_time(timespan, precision=3):

  • """Formats the timespan in a human readable form"""
  • import math
  • XXX: Unfortunately the unicode 'micro' symbol can cause problems in

  • certain terminals. Until we figure out a robust way of

  • auto-detecting if the terminal can deal with it, use plain 'us' for

  • microseconds. I am really NOT happy about disabling the proper

  • 'micro' prefix, but crashing is worse... If anyone knows what the

  • right solution for this is, I'm all ears...

I just read https://bugs.launchpad.net/ipython/+bug/348466 And I'm not
sure what happens here: if some user has the terminal encoding set to
ascii, it will result in an unicode error.

For example eclipse set the default stdout encoding in the embedded
console to None but print works...:

import sys>>> print(sys.stdout.encoding)None>>> print(u'µ'.encode(sys.stdout.encoding))Traceback (most recent call last):
File "", line 1, in TypeError: encode() argument 1 must be string, not None>>> print(u'µ')µ

I think the only "working" solution would be to try to print a "µ" and if
hat fails use "u" instead, but I've no idea how to do that without actually
printing a char to the console...


Reply to this email directly or view it on GitHubhttps://github.com//pull/2855/files#r2870793.

@jankatins
Copy link
Contributor Author

Another implementation, which does hour:sec formatting: http://snipplr.com/view/5713/

The unicode version is guarded so that it won't crash in terminals which
do not support that sign.

Signed-off-by: Jan Schulz <jasc@gmx.net>
@jankatins
Copy link
Contributor Author

I've added a commit to print real "µs" instead of "us". Please test as I'm not sure if that won't break on funny terminals...

@minrk
Copy link
Member

minrk commented Feb 3, 2013

speaking of funny terminals, Travis apparently uses an ascii locale.

@minrk
Copy link
Member

minrk commented Feb 3, 2013

So one more case is when stdout is overridden with a StringIO, in which case it may have no encoding attribute.
This should be taken care of if you use utils.encoding.get_stream_enc(sys.stdout), which catches that sort of thing.

time is now reported as '0 ns' and not '0.00 s'

Signed-off-by: Jan Schulz <jasc@gmx.net>
Signed-off-by: Jan Schulz <jasc@gmx.net>
Signed-off-by: Jan Schulz <jasc@gmx.net>
@minrk
Copy link
Member

minrk commented Feb 4, 2013

Looks good to me now.

@takluyver
Copy link
Member

This works in my tests, so I'll merge it. Thanks!

takluyver added a commit that referenced this pull request Feb 5, 2013
time(it) magic: Implement minutes/hour formatting and "%%time" cell magic
@takluyver takluyver merged commit 50cb32f into ipython:master Feb 5, 2013
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this pull request Nov 3, 2014
time(it) magic: Implement minutes/hour formatting and "%%time" cell magic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

%%timeit should use minutes to format running time in long running cells
4 participants