Skip to content

Commit e9407cf

Browse files
committed
rb_ary_push benchmark
1 parent 866f1a1 commit e9407cf

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

array.c

+16
Original file line numberDiff line numberDiff line change
@@ -8219,6 +8219,20 @@ rb_ary_deconstruct(VALUE ary)
82198219
return ary;
82208220
}
82218221

8222+
static VALUE bench_ary_push(VALUE self, VALUE vsize, VALUE viter)
8223+
{
8224+
long size = NUM2LONG(vsize);
8225+
VALUE ary = rb_ary_new2(size);
8226+
long iter = NUM2LONG(viter);
8227+
for (long index = 0; index < iter; index++) {
8228+
for (long i = 0; i < size; i++) {
8229+
rb_ary_push(ary, Qfalse);
8230+
}
8231+
rb_ary_clear(ary);
8232+
}
8233+
return Qnil;
8234+
}
8235+
82228236
/*
82238237
* An \Array object is an ordered, integer-indexed collection of objects,
82248238
* called _elements_;
@@ -8730,6 +8744,8 @@ Init_Array(void)
87308744
rb_cArray = rb_define_class("Array", rb_cObject);
87318745
rb_include_module(rb_cArray, rb_mEnumerable);
87328746

8747+
rb_define_singleton_method(rb_cArray, "bench_ary_push", bench_ary_push, 2);
8748+
87338749
rb_define_alloc_func(rb_cArray, empty_ary_alloc);
87348750
rb_define_singleton_method(rb_cArray, "new", rb_ary_s_new, -1);
87358751
rb_define_singleton_method(rb_cArray, "[]", rb_ary_s_create, -1);

benchmark/rb_ary_push.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
benchmark:
2+
"64*2k": Array.bench_ary_push(128, 2_000)
3+
"128*1k": Array.bench_ary_push(128, 1_000)

0 commit comments

Comments
 (0)