File tree 3 files changed +84
-0
lines changed 3 files changed +84
-0
lines changed Original file line number Diff line number Diff line change @@ -19,3 +19,17 @@ fn iter_dict(b: &mut Bencher) {
19
19
}
20
20
} ) ;
21
21
}
22
+
23
+ #[ bench]
24
+ fn dict_get_item ( b : & mut Bencher ) {
25
+ let gil = Python :: acquire_gil ( ) ;
26
+ let py = gil. python ( ) ;
27
+ const LEN : usize = 50_000 ;
28
+ let dict = ( 0 ..LEN as u64 ) . map ( |i| ( i, i * 2 ) ) . into_py_dict ( py) ;
29
+ let mut sum = 0 ;
30
+ b. iter ( || {
31
+ for i in 0 ..LEN {
32
+ sum += dict. get_item ( i) . unwrap ( ) . extract :: < usize > ( ) . unwrap ( ) ;
33
+ }
34
+ } ) ;
35
+ }
Original file line number Diff line number Diff line change
1
+ #![ feature( test) ]
2
+
3
+ extern crate test;
4
+ use pyo3:: prelude:: * ;
5
+ use pyo3:: types:: PyList ;
6
+ use test:: Bencher ;
7
+
8
+ #[ bench]
9
+ fn iter_list ( b : & mut Bencher ) {
10
+ let gil = Python :: acquire_gil ( ) ;
11
+ let py = gil. python ( ) ;
12
+ const LEN : usize = 100_000 ;
13
+ let list = PyList :: new ( py, 0 ..LEN ) ;
14
+ let mut sum = 0 ;
15
+ b. iter ( || {
16
+ for x in list. iter ( ) {
17
+ let i: u64 = x. extract ( ) . unwrap ( ) ;
18
+ sum += i;
19
+ }
20
+ } ) ;
21
+ }
22
+
23
+ #[ bench]
24
+ fn list_get_item ( b : & mut Bencher ) {
25
+ let gil = Python :: acquire_gil ( ) ;
26
+ let py = gil. python ( ) ;
27
+ const LEN : usize = 50_000 ;
28
+ let list = PyList :: new ( py, 0 ..LEN ) ;
29
+ let mut sum = 0 ;
30
+ b. iter ( || {
31
+ for i in 0 ..LEN {
32
+ sum += list. get_item ( i as isize ) . extract :: < usize > ( ) . unwrap ( ) ;
33
+ }
34
+ } ) ;
35
+ }
Original file line number Diff line number Diff line change
1
+ #![ feature( test) ]
2
+
3
+ extern crate test;
4
+ use pyo3:: prelude:: * ;
5
+ use pyo3:: types:: PyTuple ;
6
+ use test:: Bencher ;
7
+
8
+ #[ bench]
9
+ fn iter_tuple ( b : & mut Bencher ) {
10
+ let gil = Python :: acquire_gil ( ) ;
11
+ let py = gil. python ( ) ;
12
+ const LEN : usize = 100_000 ;
13
+ let tuple = PyTuple :: new ( py, 0 ..LEN ) ;
14
+ let mut sum = 0 ;
15
+ b. iter ( || {
16
+ for x in tuple. iter ( ) {
17
+ let i: u64 = x. extract ( ) . unwrap ( ) ;
18
+ sum += i;
19
+ }
20
+ } ) ;
21
+ }
22
+
23
+ #[ bench]
24
+ fn tuple_get_item ( b : & mut Bencher ) {
25
+ let gil = Python :: acquire_gil ( ) ;
26
+ let py = gil. python ( ) ;
27
+ const LEN : usize = 50_000 ;
28
+ let tuple = PyTuple :: new ( py, 0 ..LEN ) ;
29
+ let mut sum = 0 ;
30
+ b. iter ( || {
31
+ for i in 0 ..LEN {
32
+ sum += tuple. get_item ( i) . extract :: < usize > ( ) . unwrap ( ) ;
33
+ }
34
+ } ) ;
35
+ }
You can’t perform that action at this time.
0 commit comments