Skip to content

Performance.md

Trent M. Wyatt edited this page Apr 21, 2025 · 1 revision

Performance

Want to make Bang scream with speed while keeping your Arduino’s command-line domination on point? This page dives into performance factors for ! (shell), & (sketch uploads), and @ (macro) commands, ensuring your BANG("echo Hello") or BANG("&blink\n") runs like a champ. From serial speeds to host command latency, we’ll optimize Bang to stay lean for Arduino’s tight memory and maximize your project’s punch. Let’s rev up this beast!

Overview

  • Purpose: Optimize Bang’s performance for Arduino and host interaction.
  • Key Factors:
    • Serial-USB communication speed (baud rate).
    • Host command execution latency (! and @).
    • Sketch upload time (&).
    • Arduino memory usage.
  • Goal: Balance speed and reliability for smooth operation.

Performance Factors

1. Serial Baud Rate

  • Impact: Higher baud rates (e.g., 115200 vs. 38400) reduce command/response latency but may increase errors on some Arduino boards.
  • Default: 38400 baud.
  • Optimization:
    • Increase baud rate in sketch and arduino_exec.py:

      void setup() {
          Serial.begin(115200); // Faster than 38400
          while (!Serial) ;
      }

      Run arduino_exec.py with matching rate:

      sudo python3 arduino_exec.py -p /dev/cu.usbserial-00100 -b 115200
    • Test Stability: Some boards (e.g., Uno) may need lower rates (e.g., 57600) for reliability.

    • Tip: Match baud rates exactly to avoid communication errors.

2. Host Command Latency

  • Impact: ! (shell) and @ (macro) commands depend on host shell or script execution speed.
  • Optimization:
    • Use simple shell commands:

      BANG("echo Hello"); // Faster than complex scripts
    • Predefine macros for frequent commands:

      macros = {'weather': 'curl wttr.in?format=3'}  # In arduino_exec.py

      Then: BANG("@weather") (faster than BANG("curl wttr.in?format=3")).

    • Minimize host load: Close unnecessary apps to speed up command execution.

    • Cache results for repetitive macros if supported in arduino_exec.py.

3. Sketch Upload Time

  • Impact: &sketch_name\n uploads involve compiling and flashing, which can take seconds.
  • Optimization:
    • Use small sketches to reduce compile/upload time:

      // In blink.ino
      #include <Bang.h>
      Bang bang(Serial);
      void setup() {
          Serial.begin(38400);
          pinMode(13, OUTPUT);
      }
      void loop() {
          digitalWrite(13, HIGH);
          delay(1000);
          digitalWrite(13, LOW);
          delay(1000);
      }
    • Optimize arduino-cli:

      arduino-cli compile --fqbn arduino:avr:uno blink.ino --optimize-for-size
    • Precompile sketches if possible to reduce upload time.

    • Ensure fast host CPU and SSD for quicker compilation.

4. Arduino Memory Usage

  • Impact: Bang’s lean design minimizes RAM usage, but complex sketches or many macros can strain Arduino’s memory.
  • Optimization:
    • Use BANG_P for static commands to save RAM:

      BANG_P(PSTR("echo Hello")); // Stored in PROGMEM
    • Limit String usage:

      // Avoid
      String cmd = "curl https://example.com";
      BANG_S(cmd);
      // Prefer
      BANG("curl https://example.com");
    • Keep sketches minimal to leave room for Bang’s operations.

    • Monitor memory with Arduino IDE’s compiler output.

Testing Performance

  • Measure Latency:

    • Add timing in sketch:

      unsigned long start = millis();
      BANG("echo Hello");
      Serial.print("Latency: ");
      Serial.println(millis() - start);
  • Log Upload Time:

    • Modify arduino_exec.py:

      def compile_and_upload(sketch):
          start_time = time.time()
          # Upload logic
          print(f"Upload took {time.time() - start_time} seconds")
  • Check Macro Speed:

    • Test @macro vs. direct ! commands:

      BANG("@weather"); // Should be faster than BANG("curl wttr.in?format=3")

Troubleshooting

  • Slow Commands: Lower baud rate (e.g., 57600) or simplify commands.
  • Upload Delays: Check arduino-cli logs or host CPU load.
  • Memory Issues: Use BANG_P and minimize String objects.
  • See Troubleshooting for more fixes.

Next Steps

  • Check Testing for validating performance tweaks.
  • Explore Examples for optimized sketches.
  • See FAQ for performance questions.

Back to Home | Next: Testing

Clone this wiki locally