Skip to content

Quick Start

Open In Colab

There are 4 ways to benchmark with easybench:

  1. @bench decorator

    from easybench import bench
    
    # Add @bench with function parameters
    @bench(item=123, big_list=lambda: list(range(1_000_000)))
    def insert_first(item, big_list):
        big_list.insert(0, item)
    

    Tip

    When you need fresh data for each trial, use a function or lambda to generate new data on demand.
    (like lambda: list(range(1_000_000)) in the above)

  2. EasyBench class

    from easybench import EasyBench, BenchConfig
    
    class BenchListOperation(EasyBench):
        # Benchmark configuration
        bench_config = BenchConfig(
            trials=10,
            memory=True,
            sort_by="avg"
        )
    
        # Setup for each trial
        def setup_trial(self):
            self.big_list = list(range(1_000_000))
    
        # Benchmark methods (must start with bench_)
        def bench_insert_first(self):
            self.big_list.insert(0, 123)
    
        # You can define multiple benchmark methods
        def bench_pop_first(self):
            self.big_list.pop(0)
    
    if __name__ == "__main__":
        # Run benchmark
        BenchListOperation().bench()
    
  3. easybench command

    1. Create a benchmarks directory
    2. Put bench_*.py scripts in the directory:

      from easybench import fixture
      
      # Fixture for each trial
      @fixture(scope="trial")
      def big_list():
          return list(range(1_000_000))
      
      # Benchmark functions (must start with bench_)
      def bench_insert_first(big_list):
          big_list.insert(0, 123)
      
      # You can define multiple benchmark functions
      def bench_pop_first(big_list):
          big_list.pop(0)
      
    3. Run easybench command

      easybench --trials 10 --memory --sort-by avg
      
  4. %%easybench magic command (Jupyter Notebook)

    1. Load the extension

      %load_ext easybench
      
    2. Benchmark cell code

      %%easybench --trials=3 --memory
      result = []
      for i in range(1_000_000):
          result.append(i)
      

Example of benchmark results:

  • Single benchmark

    Benchmark Results (5 trials):
    
    Function        Avg Time (s)  Min Time (s)  Max Time (s)
    --------------------------------------------------------
    insert_first        0.001568      0.001071      0.003265
    
  • Multiple benchmarks

    EasyBench Benchmark Result

  • Boxplot Visualization

    Boxplot Visualization

  • Violinplot Visualization

    Violinplot Visualization

  • Lineplot Visualization

    Lineplot Visualization

  • Histogram Visualization

    Histplot Visualization

  • Barplot Visualization

    Barplot Visualization