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

%%timeit working, but %%time isn't #13057

Open
PurneswarPrasad opened this issue Jul 13, 2021 · 3 comments
Open

%%timeit working, but %%time isn't #13057

PurneswarPrasad opened this issue Jul 13, 2021 · 3 comments
Labels

Comments

@PurneswarPrasad
Copy link

I was running this particular code where the lambda function sends data to a function to get its full-form from it's abbreviated form.
I used %%time first to know the time taken in the block, but got an error. %%timeit solved it tho. Why does it happen?
Here is a screenshot:
Screenshot (19)

@eyenpi
Copy link
Contributor

eyenpi commented Jul 14, 2021

It happens because of this if statement in time magic function.

if line and cell:
    raise UsageError("Can't use statement directly after '%%time'!")

The line variable gets the comment and the cell variable gets the next line expression. I didn't see this check in timeit magic. Timeit just counts that line as cell expression and run it. So is timeit magic doing it right or time magic? Or it can be just an issue with parsing comments?

@PurneswarPrasad
Copy link
Author

@eyenpi the tutorial which I was following did write the code in the same cell after %%time and it ran. Idk why it didn't tho on my pc.

@eyenpi
Copy link
Contributor

eyenpi commented Jul 14, 2021

@PurneswarPrasad Maybe you are using a deprecated tutorial. As it says in the documentation if a statement comes in the first line of the %%time magic, it will raise an error. (comments will be count as statements)
https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-time
But in %%timeit the statement in the first line is used as setup code.
https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-timeit
So I think there is no problem with the implementation now. But it could be better to check for comments and don't raise errors in the case that comments appear in the first line.

@MrMino MrMino added the magics label Mar 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants