Skip to content

[Bug] pybricksdev is very slow to run a script of 200 lines #283

Closed

Description

@Novakasa

Describe the bug
Coming from #282, I measured the time for BLEPUPConnection.run(script, wait=False) to complete to be close to 10 seconds. This seems excessive for the task. Running the same script using Pybricks Code takes on average 2-3 seconds for me.

To reproduce
Run the following script for a PoweredUp City hub:

import time
import asyncio
from pybricksdev.connections import BLEPUPConnection
from pybricksdev.ble import find_device

async def main_coro():
    device = await find_device("Pybricks Hub")
    hub = BLEPUPConnection()
    await hub.connect(device)
    t0 = time.time()
    await hub.run("autonomous_train.py", wait=False)
    t = time.time()-t0
    print(f"starting program took {t} seconds!")

asyncio.run(main_coro())

The output gives me:

Searching for Pybricks Hub
Connecting to 90:84:2B:BE:1E:40: Pybricks Hub
Connected successfully!
starting program took 9.359002113342285 seconds!

Expected behavior
Given that running the same script using Pybricks Code takes just about 2-3 seconds, I would expect the process to be as fast using pybricksdev

Activity

  1. added
    triage
    on Mar 27, 2021
  2. added
    software: pybricksdev
    and removed
    triage
    on Mar 27, 2021
  3. laurensvalk commented on Mar 28, 2021

    @laurensvalk
    Member

    We are revising the program download process extensively at the moment. Once that is complete, pybricksdev can be updated to match, at which point we could look at the download speed as well.

  4. laurensvalk commented on Mar 31, 2021

    @laurensvalk
    Member

    Pybricksdev has been updated to match the new download and run process. Just get the latest commit from master.

    You'll need the latest firmware to try it.

    I have only tried one hub for now and have not benchmarked extensively, but download progress should be much faster now. All waits have been removed, so this might be about as fast as Pybricks Code now.

  5. laurensvalk commented on Mar 31, 2021

    @laurensvalk
    Member

    It has also not been tested in notebooks and with multiple programs yet, so things might be a bit flaky for now. It probably also doesn't work with Move Hub just yet.

    Feel free to share issues if you run into them.

  6. Novakasa commented on Apr 1, 2021

    @Novakasa
    SponsorAuthor

    Great, thank you!

    I checked out latest master commit, flashed to the most recent build using Pybricks Code, and adapted the above example to PybricksHub. This time, I measured a running time of just over 6 seconds. That's a great improvement!

    I wanted to test running the same script using Pybricks Code now, but it doesn't seem to work with this firmware yet, I'm getting Incompatible .mpy file in the Pybricks Code console right after connecting, and the run button is greyed out even when connected to the hub. Same when only loading a hello world script. However, running on another hub with older firmware using Pybricks Code also takes about 6 second when manually timing it. So the run time from pybricks code seems to be matched now! In my original post, I might have underestimated the time for Pybricks Code to run the script.

    I will need to update my scripts for any changed pybricks API now, as it seems to quit immediately now. (I need to work on making any errors show up for me)

  7. dlech commented on Apr 1, 2021

    @dlech
    Member

    Hopefully we will update https://code.pybricks.com within the next week, but if you are really anxious, you can build and run Pybricks Code locally (instructions are in the GitHub repo with the source code).

  8. dlech commented on Apr 9, 2021

    @dlech
    Member

    This time, I measured a running time of just over 6 seconds.

    I assume this includes connecting, running and disconnecting the hub. If you just connect once and stay connected, running should be much faster.

  9. Novakasa commented on Apr 10, 2021

    @Novakasa
    SponsorAuthor

    @dlech No, for testing the pybricksdev download speed I actually used the same method of timing it as outlined in the first post of this issue. (Though now with the new PybricksHub class instead of BLEPUPConnection)

    I just tested again using the updated pybricks code app and measured around 5.5 seconds for a program that I adapted to the new firmware. I timed it after having connected to the hub, from clicking on the run button on pybricks code until "Running downloaded program..." is printed.
    The program I am downloading (though that should probably not matter at all): https://pastebin.com/snvpWVU5

    I will probably test it on my laptop soon as well.

  10. Novakasa commented on Apr 10, 2021

    @Novakasa
    SponsorAuthor

    I just tested with pybricks code on my laptop using it's built in bluetooth adapter, also came out as around 6 seconds.

    I tested on both my Desktop PC and on my laptop with Chrome and on Microsoft Edge, in all cases the results were consistent.

    Both my machines are Windows 10, but I have also Linux installed, I might try that soon.

  11. Novakasa commented on Apr 10, 2021

    @Novakasa
    SponsorAuthor

    As a comparison, I also measured the time it takes to flash the firmware. I started the measurement when the progress bar in pybricks code shows up and stopped when it is complete. Came out as 73 seconds. I had the "Include current program" option enabled. I also measured without "Include current program" and the time was 70 seconds.

  12. Novakasa commented on Apr 10, 2021

    @Novakasa
    SponsorAuthor

    I tested the download speed on my Linux Mint install now. I used pybricks code in Chrome.
    Interestingly, I pretty consistently measure 4.5 seconds, which is on average 1 second faster than on windows on the same machine.

    I also want to note that on both windows and linux, I got the "incompatible .mpy file" error (#290 ) from time to time, which I could only fix by restarting the browser.

  13. dlech commented on Apr 12, 2021

    @dlech
    Member

    I'm getting similar download times for the pasebin program, so I think we can close this unless pybricksdev is still way slower than Pybricks Code.

  14. Novakasa commented on Apr 12, 2021

    @Novakasa
    SponsorAuthor

    No, the different download times were fixed with last week's changes.
    Thanks for the confirmation of the download times!

  15. added theissue type on Aug 6, 2025
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

    software: pybricksdev

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions