Skip to content

python3 is very slow to start #204

Closed

Description

@dwalton76

Does it take forever to start a python3 program on an ev3 for anyone else? It takes more than 30s for me to start any program for controlling a robot. Even hello world is taking 6+ seconds.

robot@ev3dev[~]# cat hello_world.py 
#!/usr/bin/env python3

print("hello world")
robot@ev3dev[~]# 
robot@ev3dev[~]# python3 --version
Python 3.4.2
robot@ev3dev[~]# 
robot@ev3dev[~]# time ./hello_world.py 
hello world

real    0m6.497s
user    0m2.030s
sys 0m0.340s
robot@ev3dev[~]# 

Activity

  1. WasabiFan commented on Sep 18, 2016

    @WasabiFan
    Member

    I'm seeing slightly faster times (four to five seconds for the same test you showed) but either way it is slower than would be ideal.

    Some additional testing: adding m = LargeMotor() and the appropriate import increased the time to around ten seconds, whereas adding just the import barely affected it, so it looks like actually doing something of use also increased the time significantly. A second initialization of a LargeMotor did not increase the time.

  2. dwalton76 commented on Sep 18, 2016

    @dwalton76
    CollaboratorAuthor

    I ran it a few more times and got between 3s and 4s each time, I'm not sure what happened with the 6s run. I changed it to:

    robot@ev3dev[~]# cat hello_world.py 
    #!/usr/bin/env python3
    
    from ev3dev.auto import LargeMotor
    m = LargeMotor()
    
    print("hello world")
    robot@ev3dev[~]# 
    robot@ev3dev[~]# time ./hello_world.py 
    hello world
    
    real    0m48.315s
    user    0m30.220s
    sys 0m2.170s
    robot@ev3dev[~]# 
    
    

    It took 48 seconds!! :( @WasabiFan what do you have for 'python --version'?

    Profiling shows that we are spending a huge amount of time importing PIL

            1    0.001    0.001   26.001   26.001 pkg_resources.py:951(resource_filename)
         12/2    4.665    0.389   57.503   28.752 {method 'load_module' of 'zipimport.zipimporter' objects}
            1    0.002    0.002   41.423   41.423 _imaging.py:1(__bootstrap__)
            1    0.002    0.002   41.425   41.425 _imaging.py:1(<module>)
            1    0.016    0.016   42.805   42.805 Image.py:27(<module>)
            1    0.016    0.016   48.708   48.708 core.py:33(<module>)
            1    0.003    0.003   53.658   53.658 ev3.py:27(<module>)
            1    0.005    0.005   57.476   57.476 auto.py:1(<module>)
         90/1    0.085    0.001   57.618   57.618 <frozen importlib._bootstrap>:2207(_find_and_load_unlocked)
         90/1    0.120    0.001   57.625   57.625 <frozen importlib._bootstrap>:2234(_find_and_load)
            1    0.002    0.002   57.677   57.677 hello_world.py:3(<module>)
         63/1    0.475    0.008   57.677   57.677 {built-in method exec}
       ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    

    If I only import PIL in the Screen init my hello_world drops to 11s

    robot@ev3dev[~]# time ./hello_world.py 
    hello world
    
    real    0m11.798s
    user    0m7.930s
    sys 0m0.630s
    robot@ev3dev[~]# 
    
  3. WasabiFan commented on Sep 19, 2016

    @WasabiFan
    Member

    Hmmm... I still can't replicate the 30-second-plus ones. I performed the exact same test (with the same Python 3 version) and I get 9-10 seconds. So, I suspect that whatever is taking a long time while importing PIL is not happening on my end, because my normal times are the same as those that you get when you rearrange the import.

  4. dwalton76 commented on Sep 20, 2016

    @dwalton76
    CollaboratorAuthor

    I must have done something odd to PIL on my install at some point. I checked in some small changes. I didn't spot anything else obvious to fix to speed this up.

  5. WasabiFan commented on Sep 20, 2016

    @WasabiFan
    Member

    Ok, sounds good. Glad we got it sorted out! And that import optimization that you made will be good regardless the exact time it took.

  6. codeNinjaDev commented on Jan 12, 2018

    @codeNinjaDev

    Is there a way to speed this up because I am using this at a timed competition.

  7. ensonic commented on Jan 12, 2018

    @ensonic
    Contributor
  8. Slidr666 commented on Jan 18, 2018

    @Slidr666

    Hi ensonic
    am i right the important lines are:

    [...]
    compiled_code = compile(code, filename, 'exec') exec(compiled_code, scope)
    [...]

    I´ve tried this using py_compile - package loading my_script.py as filename.
    It runs but is as slow as before.
    What exactly is the hack?
    My current testing cycles of the code are way too long so this would really help to get things ready faster.

    Thanks!

  9. ensonic commented on Jan 18, 2018

    @ensonic
    Contributor

    The hack is to pre-start a python program and then use the:

    compiled_code = compile(code, filename, 'exec')
    exec(compiled_code, scope)
    

    to start the program from that process.

  10. Slidr666 commented on Jan 18, 2018

    @Slidr666

    thx. is the actual code inside the variable code or inside filename.
    which package delivers the "compile"-function? currently i cant sort it out. sorry if i missed something obvious... i´m just getting started into that stuff.

  11. ensonic commented on Jan 19, 2018

    @ensonic
    Contributor

    The ticket is probably not the right place to discuss. Lets try the chat-room: https://gitter.im/ev3dev/chat

  12. Atzingen commented on Sep 21, 2018

    @Atzingen

    hello @ensonic how do you "compile" your python script ? Are you using regular python3.6 or Cython ?

  13. Atzingen commented on Sep 21, 2018

    @Atzingen

    The hack is to pre-start a python program and then use the:

    compiled_code = compile(code, filename, 'exec')
    exec(compiled_code, scope)
    

    to start the program from that process.

    Can you elaborate more on how do to do this ?

  14. scprotz commented on Oct 26, 2018

    @scprotz

    Python3 still runs slow and I tried the 'exec' hack (but it doesn't seem to help).

    I also tried to manually compile my .py files to .pyc using python3 -m compileall .

    It ran with no errors but no .pyc files were created. I have the feeling that the interim bytecode version isn't stored and thus making it very slow to run the programs each time.

    Can anyone with more knowledge about this please post? Thanks

  15. 4 remaining items

  16. Dilbert76 commented on Nov 25, 2018

    @Dilbert76

    Hi @dwalton76 , great hint. However I'm failing importing ev3dev or (preferred) ev3dev2. Apparently micropython uses a different path than python3, hence micropython cannot find the ev3dev libraries. I guess there's a trick how to make EV3 motors and sensors work with micropython but I can't find it.
    Please advise...

  17. WasabiFan commented on Nov 25, 2018

    @WasabiFan
    Member

    We have not actually released proper micropython support yet; the code changes are there but packaging isn't. If you'd like to, it is possible to manually copy the library files where they need to go, but my suggestion is to wait until we have a release with the correct packaging.

  18. Dilbert76 commented on Nov 26, 2018

    @Dilbert76
  19. WasabiFan commented on Nov 26, 2018

    @WasabiFan
    Member

    Yes, this will only apply to the ev3dev2 module. It'll be whenever I get some time to set up the packaging — hopefully this week/weekend now that I got my packaging VM in a functional state again.

  20. Dilbert76 commented on Nov 27, 2018

    @Dilbert76
  21. dwalton76 commented on Nov 27, 2018

    @dwalton76
    CollaboratorAuthor

    David figured out a way to overclock the CPU, that might be worth trying:
    https://lechnology.com/2018/06/overclocking-lego-mindstorms-ev3-part-2/

    Another option would be to use one single program in the competition so that you only have to pay that 2s penalty once at the very beginning. You could then use a menu to tell your program which chunk of code to execute next.

  22. Dilbert76 commented on Nov 29, 2018

    @Dilbert76
  23. WasabiFan commented on Nov 29, 2018

    @WasabiFan
    Member

    Currently only motors and sensors are supported, sadly 😢. Documentation will come along with proper micropython packaging (I'm working on this right now). If button support is important to you, please open an issue about it at https://github.com/ev3dev/ev3dev-lang-python so we can track it. (or, if you're feeling particularly motivated, see if you can make it work yourself and submit a pull request 😄)

  24. Atzingen commented on Feb 20, 2019

    @Atzingen

    Hello @WasabiFan and @Dilbert76 . I', having the same problem of slow import on regular python3. Can you guys help in with the micropython and library instalation on the ev3dev brick ?
    Thanks alot

  25. WasabiFan commented on Feb 20, 2019

    @WasabiFan
    Member

    Try sudo apt install micropython-ev3dev2. I haven't gotten official instructions written yet but that should be the main thing you need to do.

  26. fractal13 commented on Mar 21, 2019

    @fractal13
    Contributor

    My timing tests show about 3-4 seconds for python3 to start, and an additional 8-9 seconds for the following imports:

    import ev3dev2.sensor.lego
    import ev3dev2.display
    import ev3dev2.fonts
    import ev3dev2.motor

  27. scprotz commented on Sep 22, 2019

    @scprotz

    So after Lego released their pybrick micropython version, it appears that the weak spot in python and micropython isn't them running the code, or even really booting up (as much), but rather they each load a significant amount of files. Since we are running off SD cards, this has a very huge impact. I ran across this recently as I was working on building a new Javascript SDK and upon reflection noticed that Lego's version of the API is much faster than the default because they have bundled most of it into a single file. In my Javascript SDK, when I do testing and load lots of files individually, it takes very long on the actual robot (on my desktop it is nearly instant). I wrote a small script to merge all the JS files into a single monolithic file, and it was night and day. I was able to run things as fast as a C or Bash program (about 1 second from pushing start to it executing commands).

    Could we get a release that is monolithic (all one file) for Micropython? I bet we could get it to run in under 1 or 2 seconds from start then.

  28. WasabiFan commented on Sep 22, 2019

    @WasabiFan
    Member

    I'd be interested to see benchmarks on our codebase. I'm not immediately sure how to pack Python files together, considering the module system, but if someone could figure out how to set up a test and show comparisons that would be very helpful.

  29. bpmerkel commented on Sep 22, 2019

    @bpmerkel
    Contributor

    I'm especially intrigued by @QuriyKort's single-file ev3fast.py implementation; https://github.com/QuirkyCort/ev3dev-lang-python-fast. See his README for the benchmarks he ran.

  30. scprotz commented on Sep 22, 2019

    @scprotz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions