Skip to content

Commit

Permalink
improve a performance in Array#hash
Browse files Browse the repository at this point in the history
* before
      user     system      total        real
  1.350000   0.010000   1.360000 (  1.356039)

* after
      user     system      total        real
  0.800000   0.000000   0.800000 (  0.806837)

Test Code
----
Benchmark.bm do |x|
  ary = ["a", "b", "c"] * 10000

  x.report do
    1000.times do
      ary.hash
    end
  end
end
  • Loading branch information
Watson1978 committed Jan 26, 2013
1 parent cbc4ed4 commit d183b19
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions NSArray.m
Original file line number Diff line number Diff line change
Expand Up @@ -1579,17 +1579,19 @@
static unsigned long
recursive_hash(VALUE ary, VALUE dummy, int recur)
{
long i;
long i, len;
st_index_t h;
VALUE n;

h = rb_hash_start(RARRAY_LEN(ary));
len = RARRAY_LEN(ary);
h = rb_hash_start(len);
if (recur) {
h = rb_hash_uint(h, NUM2LONG(rb_hash(rb_cArray)));
}
else {
for (i=0; i<RARRAY_LEN(ary); i++) {
n = rb_hash(RARRAY_PTR(ary)[i]);
const VALUE *p = RARRAY_PTR(ary);
for (i = 0; i < len; i++) {
n = rb_hash(p[i]);
h = rb_hash_uint(h, NUM2LONG(n));
}
}
Expand Down

0 comments on commit d183b19

Please sign in to comment.