bondscell_results$61f9ac14-0f10-11f0-3773-89939e07eb17queued¤logsrunning¦outputbody

Benchmark a function

To benchmark our program one of the best ways is to use the BenchmarkTools library of julia. It has 6 main macros that can be used:

Each of them supports different optional variables, the most used are usually samples to decide on how many samples the benchmark should be performed, and evals which sets how many evaluations should be performed for each sample

mimetext/htmlrootassigneelast_run_timestampAj0=~persist_js_state·has_pluto_hook_features§cell_id$61f9ac14-0f10-11f0-3773-89939e07eb17depends_on_disabled_cells§runtime published_object_keysdepends_on_skipped_cells§errored$0bb623c7-9e17-4d79-bea7-ee8fa265f80fqueued¤logslinemsg' 486.680 ns (7 allocations: 1.88 KiB) text/plaincell_id$0bb623c7-9e17-4d79-bea7-ee8fa265f80fkwargsidPlutoRunner_d1acb81efileP/home/runner/.julia/packages/Pluto/5ete1/src/runner/PlutoRunner/src/io/stdout.jlgroupstdoutlevelLogLevel(-555)running¦outputbodymimetext/plainrootassigneelast_run_timestampAj>/persist_js_state·has_pluto_hook_features§cell_id$0bb623c7-9e17-4d79-bea7-ee8fa265f80fdepends_on_disabled_cells§runtimeοpublished_object_keysdepends_on_skipped_cells§errored$5a546075-4c4d-413a-b182-9612488445eequeued¤logsrunning¦outputbodyBenchmarkTools.Trial: 100 samples with 10 evaluations per sample. Range (min … max): 55.876 μs … 92.216 μs ┊ GC (min … max): 0.00% … 0.00% Time (median): 86.749 μs ┊ GC (median): 0.00% Time (mean ± σ): 78.663 μs ± 13.373 μs ┊ GC (mean ± σ): 0.00% ± 0.00% █ ▅▅▆▄▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▂▁▁▁▁▁▁▁▁▁▂▁▁▁▁▁▁▁▁▁▂▁▁▁▂▃▅██▇▄▂ ▂ 55.9 μs Histogram: frequency by time 89.1 μs < Memory estimate: 78.21 KiB, allocs estimate: 3.mimetext/plainrootassigneelast_run_timestampAj=A(persist_js_state·has_pluto_hook_features§cell_id$5a546075-4c4d-413a-b182-9612488445eedepends_on_disabled_cells§runtimerpublished_object_keysdepends_on_skipped_cells§errored$37714f84-2d97-4f4c-9e82-e2f992f135d0queued¤logsrunning¦outputbody80088mimetext/plainrootassigneelast_run_timestampAj;Rpersist_js_state·has_pluto_hook_features§cell_id$37714f84-2d97-4f4c-9e82-e2f992f135d0depends_on_disabled_cells§runtimeye=published_object_keysdepends_on_skipped_cells§errored$cd37f8b2-ef67-4faf-9c82-6a49adc2e92equeued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampAj1u\persist_js_state·has_pluto_hook_features§cell_id$cd37f8b2-ef67-4faf-9c82-6a49adc2e92edepends_on_disabled_cells§runtimeΑpublished_object_keysdepends_on_skipped_cells§errored$238e34e0-baf5-472f-8513-13028afa4f8cqueued¤logsrunning¦outputbodymimetext/plainrootassigneelast_run_timestampAj=Cpersist_js_state·has_pluto_hook_features§cell_id$238e34e0-baf5-472f-8513-13028afa4f8cdepends_on_disabled_cells§runtime

Interpolate variables

If the expression you want to benchmark depends on external variables, you should use $ to "interpolate" them into the benchmark expression to avoid the problems of benchmarking with globals. Essentially, any interpolated variable $x or expression $(...) is "pre-computed" before benchmarking begins:

mimetext/htmlrootassigneelast_run_timestampAj0=Mpersist_js_state·has_pluto_hook_features§cell_id$e126c809-51f2-437d-a02a-1ce3837da351depends_on_disabled_cells§runtimeXipublished_object_keysdepends_on_skipped_cells§errored$718e76ed-7cd4-4150-a254-9730cc51d255queued¤logsrunning¦outputbody5.5163e-5mimetext/plainrootassigneelast_run_timestampAj9hpersist_js_state·has_pluto_hook_features§cell_id$718e76ed-7cd4-4150-a254-9730cc51d255depends_on_disabled_cells§runtime>published_object_keysdepends_on_skipped_cells§errored$229aba13-52b6-402d-91b0-22d74011083bqueued¤logsrunning¦outputbody3mimetext/plainrootassigneelast_run_timestampAj=%Dpersist_js_state·has_pluto_hook_features§cell_id$229aba13-52b6-402d-91b0-22d74011083bdepends_on_disabled_cells§runtimez7jdpublished_object_keysdepends_on_skipped_cells§errored$6d170aac-5357-4dc6-8c3b-0dc11e1cecbbqueued¤logslinemsg' 470.831 ns (7 allocations: 1.88 KiB) text/plaincell_id$6d170aac-5357-4dc6-8c3b-0dc11e1cecbbkwargsidPlutoRunner_d1acb81efileP/home/runner/.julia/packages/Pluto/5ete1/src/runner/PlutoRunner/src/io/stdout.jlgroupstdoutlevelLogLevel(-555)running¦outputbodymimetext/plainrootassigneelast_run_timestampAj>Tpersist_js_state·has_pluto_hook_features§cell_id$6d170aac-5357-4dc6-8c3b-0dc11e1cecbbdepends_on_disabled_cells§runtimeΔpublished_object_keysdepends_on_skipped_cells§errored±cell_dependencies$61f9ac14-0f10-11f0-3773-89939e07eb17precedence_heuristic cell_id$61f9ac14-0f10-11f0-3773-89939e07eb17downstream_cells_mapupstream_cells_map@md_strgetindex$0bb623c7-9e17-4d79-bea7-ee8fa265f80fprecedence_heuristic cell_id$0bb623c7-9e17-4d79-bea7-ee8fa265f80fdownstream_cells_mapupstream_cells_mapBenchmarkTools.ParametersC$238e34e0-baf5-472f-8513-13028afa4f8c@btime,BenchmarkTools.generate_benchmark_definitionBenchmarkTools$cd37f8b2-ef67-4faf-9c82-6a49adc2e92e##C#246==println#___this_pluto_module_nameinv$5a546075-4c4d-413a-b182-9612488445eeprecedence_heuristic cell_id$5a546075-4c4d-413a-b182-9612488445eedownstream_cells_mapupstream_cells_mapB$a16d4975-82bb-4ca7-967d-639715e4e208A$a16d4975-82bb-4ca7-967d-639715e4e208BenchmarkTools.Parameters@benchmark,BenchmarkTools.generate_benchmark_definition*BenchmarkTools$cd37f8b2-ef67-4faf-9c82-6a49adc2e92e#___this_pluto_module_name$37714f84-2d97-4f4c-9e82-e2f992f135d0precedence_heuristic cell_id$37714f84-2d97-4f4c-9e82-e2f992f135d0downstream_cells_mapupstream_cells_mapB$a16d4975-82bb-4ca7-967d-639715e4e208A$a16d4975-82bb-4ca7-967d-639715e4e208BenchmarkTools.Parameters@ballocated,BenchmarkTools.generate_benchmark_definition*BenchmarkTools$cd37f8b2-ef67-4faf-9c82-6a49adc2e92e#___this_pluto_module_name$cd37f8b2-ef67-4faf-9c82-6a49adc2e92eprecedence_heuristiccell_id$cd37f8b2-ef67-4faf-9c82-6a49adc2e92edownstream_cells_mapBenchmarkTools$028e0e39-6030-42c5-b9eb-e1aba552d53d$644a5e28-1620-4720-ba8f-35f9cff91393$f8b5e801-5d57-4c3a-aaa2-b360b1d9e951$718e76ed-7cd4-4150-a254-9730cc51d255$37714f84-2d97-4f4c-9e82-e2f992f135d0$229aba13-52b6-402d-91b0-22d74011083b$5a546075-4c4d-413a-b182-9612488445ee$0bb623c7-9e17-4d79-bea7-ee8fa265f80f$6d170aac-5357-4dc6-8c3b-0dc11e1cecbb$fed0cd42-1ad7-47f6-80bc-dfd09a689878upstream_cells_map$238e34e0-baf5-472f-8513-13028afa4f8cprecedence_heuristic cell_id$238e34e0-baf5-472f-8513-13028afa4f8cdownstream_cells_mapC$0bb623c7-9e17-4d79-bea7-ee8fa265f80fupstream_cells_maprand$fed0cd42-1ad7-47f6-80bc-dfd09a689878precedence_heuristic cell_id$fed0cd42-1ad7-47f6-80bc-dfd09a689878downstream_cells_mapupstream_cells_mapBenchmarkTools.Parameters@btime,BenchmarkTools.generate_benchmark_definitionBenchmarkTools$cd37f8b2-ef67-4faf-9c82-6a49adc2e92e#___this_pluto_module_name==randprintlninv$028e0e39-6030-42c5-b9eb-e1aba552d53dprecedence_heuristic cell_id$028e0e39-6030-42c5-b9eb-e1aba552d53ddownstream_cells_mapupstream_cells_mapB$a16d4975-82bb-4ca7-967d-639715e4e208A$a16d4975-82bb-4ca7-967d-639715e4e208BenchmarkTools.Parameters@benchmark,BenchmarkTools.generate_benchmark_definition*BenchmarkTools$cd37f8b2-ef67-4faf-9c82-6a49adc2e92e#___this_pluto_module_name$a16d4975-82bb-4ca7-967d-639715e4e208precedence_heuristic cell_id$a16d4975-82bb-4ca7-967d-639715e4e208downstream_cells_mapB$028e0e39-6030-42c5-b9eb-e1aba552d53d$644a5e28-1620-4720-ba8f-35f9cff91393$f8b5e801-5d57-4c3a-aaa2-b360b1d9e951$718e76ed-7cd4-4150-a254-9730cc51d255$37714f84-2d97-4f4c-9e82-e2f992f135d0$229aba13-52b6-402d-91b0-22d74011083b$5a546075-4c4d-413a-b182-9612488445eeA$028e0e39-6030-42c5-b9eb-e1aba552d53d$644a5e28-1620-4720-ba8f-35f9cff91393$f8b5e801-5d57-4c3a-aaa2-b360b1d9e951$718e76ed-7cd4-4150-a254-9730cc51d255$37714f84-2d97-4f4c-9e82-e2f992f135d0$229aba13-52b6-402d-91b0-22d74011083b$5a546075-4c4d-413a-b182-9612488445eeNupstream_cells_maprand$644a5e28-1620-4720-ba8f-35f9cff91393precedence_heuristic cell_id$644a5e28-1620-4720-ba8f-35f9cff91393downstream_cells_mapupstream_cells_mapB$a16d4975-82bb-4ca7-967d-639715e4e208A$a16d4975-82bb-4ca7-967d-639715e4e208BenchmarkTools.Parameters@btime,BenchmarkTools.generate_benchmark_definition*BenchmarkTools$cd37f8b2-ef67-4faf-9c82-6a49adc2e92e#___this_pluto_module_name==println$f8b5e801-5d57-4c3a-aaa2-b360b1d9e951precedence_heuristic cell_id$f8b5e801-5d57-4c3a-aaa2-b360b1d9e951downstream_cells_mapupstream_cells_mapB$a16d4975-82bb-4ca7-967d-639715e4e208A$a16d4975-82bb-4ca7-967d-639715e4e208BenchmarkTools.Parameters@btimed/,BenchmarkTools.generate_benchmark_definition*BenchmarkTools$cd37f8b2-ef67-4faf-9c82-6a49adc2e92e#___this_pluto_module_name$e126c809-51f2-437d-a02a-1ce3837da351precedence_heuristic cell_id$e126c809-51f2-437d-a02a-1ce3837da351downstream_cells_mapupstream_cells_map@md_strgetindex$718e76ed-7cd4-4150-a254-9730cc51d255precedence_heuristic cell_id$718e76ed-7cd4-4150-a254-9730cc51d255downstream_cells_mapupstream_cells_mapB$a16d4975-82bb-4ca7-967d-639715e4e208A$a16d4975-82bb-4ca7-967d-639715e4e208BenchmarkTools.Parameters/,BenchmarkTools.generate_benchmark_definition*BenchmarkTools$cd37f8b2-ef67-4faf-9c82-6a49adc2e92e#___this_pluto_module_name@belapsed$229aba13-52b6-402d-91b0-22d74011083bprecedence_heuristic cell_id$229aba13-52b6-402d-91b0-22d74011083bdownstream_cells_mapupstream_cells_map@ballocationsB$a16d4975-82bb-4ca7-967d-639715e4e208A$a16d4975-82bb-4ca7-967d-639715e4e208BenchmarkTools.Parameters,BenchmarkTools.generate_benchmark_definition*BenchmarkTools$cd37f8b2-ef67-4faf-9c82-6a49adc2e92e#___this_pluto_module_name$6d170aac-5357-4dc6-8c3b-0dc11e1cecbbprecedence_heuristic cell_id$6d170aac-5357-4dc6-8c3b-0dc11e1cecbbdownstream_cells_mapupstream_cells_mapinvBenchmarkTools.Parameters@btime,BenchmarkTools.generate_benchmark_definitionBenchmarkTools$cd37f8b2-ef67-4faf-9c82-6a49adc2e92e#___this_pluto_module_name==rand##240printlncell_execution_order$cd37f8b2-ef67-4faf-9c82-6a49adc2e92e$61f9ac14-0f10-11f0-3773-89939e07eb17$a16d4975-82bb-4ca7-967d-639715e4e208$028e0e39-6030-42c5-b9eb-e1aba552d53d$644a5e28-1620-4720-ba8f-35f9cff91393$f8b5e801-5d57-4c3a-aaa2-b360b1d9e951$718e76ed-7cd4-4150-a254-9730cc51d255$37714f84-2d97-4f4c-9e82-e2f992f135d0$229aba13-52b6-402d-91b0-22d74011083b$5a546075-4c4d-413a-b182-9612488445ee$e126c809-51f2-437d-a02a-1ce3837da351$238e34e0-baf5-472f-8513-13028afa4f8c$0bb623c7-9e17-4d79-bea7-ee8fa265f80f$6d170aac-5357-4dc6-8c3b-0dc11e1cecbb$fed0cd42-1ad7-47f6-80bc-dfd09a689878last_hot_reload_timeshortpathbenchmark_tools.jlprocess_statusreadypathP/home/runner/work/JuliaWorkshopAISF/JuliaWorkshopAISF/Lesson2/benchmark_tools.jlpluto_versionv0.20.8last_save_timeAj0;Z`cell_order$61f9ac14-0f10-11f0-3773-89939e07eb17$cd37f8b2-ef67-4faf-9c82-6a49adc2e92e$a16d4975-82bb-4ca7-967d-639715e4e208$028e0e39-6030-42c5-b9eb-e1aba552d53d$644a5e28-1620-4720-ba8f-35f9cff91393$f8b5e801-5d57-4c3a-aaa2-b360b1d9e951$718e76ed-7cd4-4150-a254-9730cc51d255$37714f84-2d97-4f4c-9e82-e2f992f135d0$229aba13-52b6-402d-91b0-22d74011083b$5a546075-4c4d-413a-b182-9612488445ee$e126c809-51f2-437d-a02a-1ce3837da351$238e34e0-baf5-472f-8513-13028afa4f8c$0bb623c7-9e17-4d79-bea7-ee8fa265f80f$6d170aac-5357-4dc6-8c3b-0dc11e1cecbb$fed0cd42-1ad7-47f6-80bc-dfd09a689878published_objectsnbpkginstall_time_nsB:instantiatedòinstalled_versionsBenchmarkTools1.6.0terminal_outputsnbpkg_sync Resolving... ===  Installed BenchmarkTools ─ v1.6.0 No Changes to `/tmp/jl_ODkYJc/Project.toml`   No Changes to `/tmp/jl_ODkYJc/Manifest.toml` Instantiating... === Precompiling... ===  Activating project at `/tmp/jl_ODkYJc` Precompiling project... 1 dependency successfully precompiled in 2 seconds. 16 already precompiled.BenchmarkTools Resolving... ===  Installed BenchmarkTools ─ v1.6.0 No Changes to `/tmp/jl_ODkYJc/Project.toml`   No Changes to `/tmp/jl_ODkYJc/Manifest.toml` Instantiating... === Precompiling... ===  Activating project at `/tmp/jl_ODkYJc` Precompiling project... 1 dependency successfully precompiled in 2 seconds. 16 already precompiled.enabled÷restart_recommended_msgrestart_required_msgbusy_packageswaiting_for_permission,waiting_for_permission_but_probably_disabled«cell_inputs$61f9ac14-0f10-11f0-3773-89939e07eb17cell_id$61f9ac14-0f10-11f0-3773-89939e07eb17codemd""" # Benchmark a function To benchmark our program one of the best ways is to use the `BenchmarkTools` library of julia. It has 6 main macros that can be used: - `@benchmark`: generic macro for benchmark - `@btime`: this executes an expression, printing the time it took to execute and the memory allocated before returning the value of the expression. - `@btimed`: this macro executes an expression and returns the value of the expression, the minimum elapsed time in seconds, the total bytes allocated, the number of allocations, and the garbage collection time in seconds during the benchmark. - `@belapsed`: this returns the minimum elapsed time (in seconds) to execute a given expression. - `@ballocated`: this returns the minimum number of bytes allocated when executing a given expression. - `@ballocations`: this macro evaluates an expression, discarding the resulting value, and returns the total number of allocations made during its execution. Each of them supports different optional variables, the most used are usually `samples` to decide on how many samples the benchmark should be performed, and `evals` which sets how many evaluations should be performed for each sample """metadatashow_logsèdisabled®skip_as_script«code_folded$0bb623c7-9e17-4d79-bea7-ee8fa265f80fcell_id$0bb623c7-9e17-4d79-bea7-ee8fa265f80fcode{@btime inv($C); # we interpolate the global variable C with $C. The variable C is found before the function inv is appliedmetadatashow_logsèdisabled®skip_as_script«code_folded$5a546075-4c4d-413a-b182-9612488445eecell_id$5a546075-4c4d-413a-b182-9612488445eecode%@benchmark A * B samples=100 evals=10metadatashow_logsèdisabled®skip_as_script«code_folded$37714f84-2d97-4f4c-9e82-e2f992f135d0cell_id$37714f84-2d97-4f4c-9e82-e2f992f135d0code@ballocated A * Bmetadatashow_logsèdisabled®skip_as_script«code_folded$cd37f8b2-ef67-4faf-9c82-6a49adc2e92ecell_id$cd37f8b2-ef67-4faf-9c82-6a49adc2e92ecodeusing BenchmarkToolsmetadatashow_logsèdisabled®skip_as_script«code_folded$238e34e0-baf5-472f-8513-13028afa4f8ccell_id$238e34e0-baf5-472f-8513-13028afa4f8ccodeC = rand(3,3);metadatashow_logsèdisabled®skip_as_script«code_folded$fed0cd42-1ad7-47f6-80bc-dfd09a689878cell_id$fed0cd42-1ad7-47f6-80bc-dfd09a689878codeQ@btime inv(rand(3,3)); # the rand(3,3) call is included in the benchmark timemetadatashow_logsèdisabled®skip_as_script«code_folded$028e0e39-6030-42c5-b9eb-e1aba552d53dcell_id$028e0e39-6030-42c5-b9eb-e1aba552d53dcode@benchmark A * Bmetadatashow_logsèdisabled®skip_as_script«code_folded$a16d4975-82bb-4ca7-967d-639715e4e208cell_id$a16d4975-82bb-4ca7-967d-639715e4e208code3begin N = 100 A = rand(N, N) B = rand(N, N) end;metadatashow_logsèdisabled®skip_as_script«code_folded$644a5e28-1620-4720-ba8f-35f9cff91393cell_id$644a5e28-1620-4720-ba8f-35f9cff91393code@btime A * Bmetadatashow_logsèdisabled®skip_as_script«code_folded$f8b5e801-5d57-4c3a-aaa2-b360b1d9e951cell_id$f8b5e801-5d57-4c3a-aaa2-b360b1d9e951code@btimed A * Bmetadatashow_logsèdisabled®skip_as_script«code_folded$e126c809-51f2-437d-a02a-1ce3837da351cell_id$e126c809-51f2-437d-a02a-1ce3837da351codePmd""" # Interpolate variables If the expression you want to benchmark depends on external variables, you should use $ to "interpolate" them into the benchmark expression to avoid the problems of benchmarking with globals. Essentially, any interpolated variable `$x` or expression $(...) is "pre-computed" before benchmarking begins: """metadatashow_logsèdisabled®skip_as_script«code_folded$718e76ed-7cd4-4150-a254-9730cc51d255cell_id$718e76ed-7cd4-4150-a254-9730cc51d255code@belapsed A * Bmetadatashow_logsèdisabled®skip_as_script«code_folded$229aba13-52b6-402d-91b0-22d74011083bcell_id$229aba13-52b6-402d-91b0-22d74011083bcode@ballocations A * Bmetadatashow_logsèdisabled®skip_as_script«code_folded$6d170aac-5357-4dc6-8c3b-0dc11e1cecbbcell_id$6d170aac-5357-4dc6-8c3b-0dc11e1cecbbcodeY@btime inv($(rand(3,3))); # interpolation: the rand(3,3) call occurs before benchmarkingmetadatashow_logsèdisabled®skip_as_script«code_folded«notebook_id$0fc35b0a-2f06-11f0-2043-9b97aa9a4c6cin_temp_dir¨metadata