11use criterion:: { criterion_group, criterion_main, BenchmarkId , Criterion } ;
2- use faiss:: { index:: { flat:: FlatIndexImpl , ivf_flat:: IVFFlatIndexImpl } , Index , ConcurrentIndex } ;
2+ use faiss:: {
3+ index:: { flat:: FlatIndexImpl , ivf_flat:: IVFFlatIndexImpl } ,
4+ ConcurrentIndex , Index ,
5+ } ;
36use rand:: Rng ;
47
5-
68pub fn generate_random_vectors ( dimension : usize , size : usize ) -> Vec < f32 > {
79 let mut rng = rand:: rng ( ) ;
810 let mut data = vec ! [ 0f32 ; dimension * size] ;
@@ -12,12 +14,16 @@ pub fn generate_random_vectors(dimension: usize, size: usize) -> Vec<f32> {
1214 data
1315}
1416
15-
16- pub fn build_index ( dimension : u32 , nlist : u32 , train_size : usize , dataset_size : usize ) -> faiss:: error:: Result < IVFFlatIndexImpl > {
17+ pub fn build_index (
18+ dimension : u32 ,
19+ nlist : u32 ,
20+ train_size : usize ,
21+ dataset_size : usize ,
22+ ) -> faiss:: error:: Result < IVFFlatIndexImpl > {
1723 let q = FlatIndexImpl :: new_l2 ( dimension) . unwrap ( ) ;
1824 let mut index = IVFFlatIndexImpl :: new_l2 ( q, dimension, nlist) . unwrap ( ) ;
1925
20- let train_vectors= generate_random_vectors ( dimension as usize , train_size) ;
26+ let train_vectors = generate_random_vectors ( dimension as usize , train_size) ;
2127 eprintln ! ( "training index..." ) ;
2228 index. train ( & train_vectors) ?;
2329
@@ -35,12 +41,7 @@ fn search_ivf_flat(c: &mut Criterion) {
3541 const DATASET_SIZE : usize = 1_000_000 ;
3642 const NEIGHBORS : usize = 25 ;
3743
38- let index = build_index (
39- DIMENSION ,
40- NLIST ,
41- TRAIN_SIZE ,
42- DATASET_SIZE
43- ) . unwrap ( ) ;
44+ let index = build_index ( DIMENSION , NLIST , TRAIN_SIZE , DATASET_SIZE ) . unwrap ( ) ;
4445 eprintln ! ( "finished building index" ) ;
4546
4647 let total_steps = 11usize ;
@@ -52,21 +53,14 @@ fn search_ivf_flat(c: &mut Criterion) {
5253
5354 let mut group = c. benchmark_group ( "search_ivf_flat" ) ;
5455 for ( idx, size) in sizes. iter ( ) . enumerate ( ) {
55- group. bench_with_input (
56- BenchmarkId :: from_parameter ( size) ,
57- size,
58- |b, & _size| {
59- b. iter ( || {
60- index. search ( & queries[ idx] , NEIGHBORS ) . unwrap ( ) ;
61- }
62- ) ;
56+ group. bench_with_input ( BenchmarkId :: from_parameter ( size) , size, |b, & _size| {
57+ b. iter ( || {
58+ index. search ( & queries[ idx] , NEIGHBORS ) . unwrap ( ) ;
59+ } ) ;
6360 } ) ;
6461 }
6562 group. finish ( ) ;
6663}
6764
68- criterion_group ! (
69- benches,
70- search_ivf_flat,
71- ) ;
65+ criterion_group ! ( benches, search_ivf_flat, ) ;
7266criterion_main ! ( benches) ;
0 commit comments