timeit in freecad

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
Jee-Bee
Veteran
Posts: 2566
Joined: Tue Jun 16, 2015 10:32 am
Location: Netherlands

timeit in freecad

Post by Jee-Bee »

I have a question related to timeit. Don't know where this question belong(here, open discussion or maybe compile).

according to https://scipy-lectures.org/advanced/opt ... x.html#id5
there are some tricks to speedup python code.

When i do the following in my default python i get this:

Code: Select all

In[2]: import timeit
In[3]: import numpy as np
In[4]: a = np.arange(100)
In[5]: %timeit a**2
1.76 µs ± 58.2 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In[6]: def b(x):
  ...:     bb = x ** 2
  ...:     return(bb)
  ...: 
In[7]: %timeit b(a)
2.26 µs ± 49.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
everything works fine

but in FreeCAD it don't work

Code: Select all

>>> import numpy as np
>>> import timeit
>>> a = np.arange(100)
>>> %timeit a ** 2
  File "<input>", line 1
    %timeit a ** 2
    ^
SyntaxError: invalid syntax
Would it be possible to make the python interpreter so it can handle this kind of functionality?


OS: OS X El Capitan (10.11)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.17175 (Git)
Build type: Release
Branch: master
Hash: 507c40669d48ae4732268324cd0140c80cf68222
Python version: 3.7.3
Qt version: 5.9.7
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/Netherlands (en_NL)

If this is not possible

what i'm doing right now looks like this

Code: Select all

import numpy as np
import scipy

def func1():
    do something


def func2():
    do func 2

def slow_func():
    slow func what needs to speedup


if __name__ == "__main__":
    a = func1()
    b = func2(a)
    
    # Here i want to do the timeit func
    timeit... ?
How do i add the timeit functionality so that it handles this kind of scripts?
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: timeit in freecad

Post by openBrain »

AFAIK "%timeit" isn't a standard Python call but a simplifier provided by the IPython shell.
I don't think that FreeCAD has to be transformed in a Python IDE. ;)
You can use the standard timeit API in FC (https://docs.python.org/3/library/timeit.html)
Notice that a code using IPython macros is not portable.

To monitor execution time of a script, you can also use the time module. @easyw posted a good example in this post. :)
User avatar
brst
Posts: 87
Joined: Thu Apr 18, 2019 10:11 am
Location: Germany

Re: timeit in freecad

Post by brst »

openBrain wrote: Tue Jul 02, 2019 7:53 pm AFAIK "%timeit" isn't a standard Python call but a simplifier provided by the IPython shell.
I don't think that FreeCAD has to be transformed in a Python IDE. ;)
You can use the standard timeit API in FC (https://docs.python.org/3/library/timeit.html)
Notice that a code using IPython macros is not portable.

To monitor execution time of a script, you can also use the time module. @easyw posted a good example in this post. :)
Yes you are right. %timeit is specific to iPython, which is used within Jupyter Notebooks.
(https://ipython.readthedocs.io/en/stabl ... agics.html)
FreeCAD rookie
Post Reply