Skip to content

Commit c77b38d

Browse files
committed
Add benches
1 parent ae7058f commit c77b38d

File tree

5 files changed

+2195
-0
lines changed

5 files changed

+2195
-0
lines changed

benches/parser.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#![feature(test)]
2+
3+
extern crate rustpython_parser;
4+
extern crate test;
5+
6+
use rustpython_parser::lexer::{make_tokenizer, Tok};
7+
use rustpython_parser::parser::parse_program;
8+
9+
#[bench]
10+
fn bench_tokenization(b: &mut test::Bencher) {
11+
let source = include_str!("../data/minidom.py");
12+
13+
b.bytes = source.len() as _;
14+
b.iter(|| {
15+
let lexer = make_tokenizer(source);
16+
lexer.map(|x| x.unwrap().1).collect::<Vec<Tok>>()
17+
})
18+
}
19+
20+
#[bench]
21+
fn bench_parse_to_ast(b: &mut test::Bencher) {
22+
let source = include_str!("../data/minidom.py");
23+
24+
b.bytes = source.len() as _;
25+
b.iter(|| parse_program(source).unwrap())
26+
}

benches/vm.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#![feature(test)]
2+
3+
extern crate rustpython_parser;
4+
extern crate rustpython_vm;
5+
extern crate test;
6+
7+
use rustpython_vm::pyobject::PyResult;
8+
use rustpython_vm::{compile, VirtualMachine};
9+
10+
#[bench]
11+
fn bench_nbody(b: &mut test::Bencher) {
12+
let source = include_str!("../data/nbody.py");
13+
14+
let vm = VirtualMachine::new();
15+
16+
let code = match compile::compile(&vm, source, &compile::Mode::Single, "<stdin>".to_string()) {
17+
Ok(code) => code,
18+
Err(e) => panic!("{:?}", e),
19+
};
20+
21+
b.iter(|| {
22+
let scope = vm.ctx.new_scope();
23+
let res: PyResult = vm.run_code_obj(code.clone(), scope);
24+
assert_eq!(res.is_ok(), true);
25+
})
26+
}
27+
28+
#[bench]
29+
fn bench_mandelbrot(b: &mut test::Bencher) {
30+
let source = include_str!("../data/mandelbrot.py");
31+
32+
let vm = VirtualMachine::new();
33+
34+
let code = match compile::compile(&vm, source, &compile::Mode::Single, "<stdin>".to_string()) {
35+
Ok(code) => code,
36+
Err(e) => panic!("{:?}", e),
37+
};
38+
39+
b.iter(|| {
40+
let scope = vm.ctx.new_scope();
41+
let res: PyResult = vm.run_code_obj(code.clone(), scope);
42+
assert_eq!(res.is_ok(), true);
43+
})
44+
}

data/mandelbrot.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python3
2+
# coding: utf-8
3+
4+
w = 50.0
5+
h = 50.0
6+
7+
y = 0.0
8+
while y < h:
9+
x = 0.0
10+
while x < w:
11+
Zr, Zi, Tr, Ti = 0.0, 0.0, 0.0, 0.0
12+
Cr = 2*x/w - 1.5
13+
Ci = 2*y/h - 1.0
14+
15+
i = 0
16+
while i < 50 and Tr+Ti <= 4:
17+
Zi = 2*Zr*Zi + Ci
18+
Zr = Tr - Ti + Cr
19+
Tr = Zr * Zr
20+
Ti = Zi * Zi
21+
i = i+1
22+
23+
if Tr+Ti <= 4:
24+
# print('*', end='')
25+
pass
26+
else:
27+
# print('·', end='')
28+
pass
29+
30+
x = x+1
31+
32+
# print()
33+
y = y+1

0 commit comments

Comments
 (0)