MCPcopy Index your code
hub / github.com/RustPython/RustPython / bench_rustpython_code

Function bench_rustpython_code

benches/microbenchmarks.rs:110–169  ·  view source on GitHub ↗
(group: &mut BenchmarkGroup<WallTime>, bench: &MicroBenchmark)

Source from the content-addressed store, hash-verified

108}
109
110fn bench_rustpython_code(group: &mut BenchmarkGroup<WallTime>, bench: &MicroBenchmark) {
111 let mut settings = Settings::default();
112 settings.path_list.push("Lib/".to_string());
113 settings.write_bytecode = false;
114 settings.user_site_directory = false;
115
116 let builder = Interpreter::builder(settings);
117 let defs = rustpython_stdlib::stdlib_module_defs(&builder.ctx);
118 let interp = builder.add_native_modules(&defs).build();
119 interp.enter(|vm| {
120 let setup_code = vm
121 .compile(&bench.setup, Mode::Exec, bench.name.to_owned())
122 .expect("Error compiling setup code");
123 let bench_code = vm
124 .compile(&bench.code, Mode::Exec, bench.name.to_owned())
125 .expect("Error compiling bench code");
126
127 let bench_func = |scope| {
128 let res: PyResult = vm.run_code_obj(bench_code.clone(), scope);
129 vm.unwrap_pyresult(res);
130 };
131
132 let bench_setup = |iterations| {
133 let scope = vm.new_scope_with_builtins();
134 if let Some(idx) = iterations {
135 scope
136 .locals
137 .as_ref()
138 .expect("new_scope_with_builtins always provides locals")
139 .as_object()
140 .set_item("ITERATIONS", vm.new_pyobj(idx), vm)
141 .expect("Error adding ITERATIONS local variable");
142 }
143 let setup_result = vm.run_code_obj(setup_code.clone(), scope.clone());
144 vm.unwrap_pyresult(setup_result);
145 scope
146 };
147
148 if bench.iterate {
149 for idx in (100..=1_000).step_by(200) {
150 group.throughput(Throughput::Elements(idx as u64));
151 group.bench_with_input(
152 BenchmarkId::new("rustpython", &bench.name),
153 &idx,
154 |b, idx| {
155 b.iter_batched(
156 || bench_setup(Some(*idx)),
157 bench_func,
158 BatchSize::LargeInput,
159 );
160 },
161 );
162 }
163 } else {
164 group.bench_function(BenchmarkId::new("rustpython", &bench.name), move |b| {
165 b.iter_batched(|| bench_setup(None), bench_func, BatchSize::LargeInput);
166 });
167 }

Callers 1

run_micro_benchmarkFunction · 0.70

Calls 15

stdlib_module_defsFunction · 0.85
newFunction · 0.85
to_stringMethod · 0.80
add_native_modulesMethod · 0.80
run_code_objMethod · 0.80
unwrap_pyresultMethod · 0.80
new_pyobjMethod · 0.80
SomeClass · 0.50
pushMethod · 0.45
buildMethod · 0.45
enterMethod · 0.45

Tested by

no test coverage detected