# What is the fastest way to iterate over a hash in C?

**URL:** <https://www.ruby-forum.com/t/what-is-the-fastest-way-to-iterate-over-a-hash-in-c/70636>\
**Category:** Ruby\
**Created:** [October 1, 2006, 7:04am UTC](https://www.ruby-forum.com/t/what-is-the-fastest-way-to-iterate-over-a-hash-in-c/70636 "2006-10-01T07:04:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bct](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6a8cbe/32.png) [@bct](https://www.ruby-forum.com/u/bct)\
**Post date:** [October 1, 2006, 7:04am UTC](https://www.ruby-forum.com/t/what-is-the-fastest-way-to-iterate-over-a-hash-in-c/70636/1 "2006-10-01T07:04:20Z")

</div>

Hello,

I’m working on the Fast JSON project, and have come against some  
puzzling  
performance quirk.

Most of the Hash#to\_json functionality is implemented in C and performs  
much  
better. However there is one section that performs 6 time better when  
implemented in ruby vs c.

I wrote a benchmark that calls to\_json on 50000 hashes.

Here is the method in Ruby. The benchmark takes around 1.7 seconds:

def process\_internal\_json(json, state, depth, delim)  
first = true  
each { |key,value|  
if first  
first = false  
else  
json \<\< delim  
end  
generate\_key\_value\_json(json, state, depth, key, value)  
}  
json  
end

Here is the method in C. The benchmark takes around 9.5 seconds:

static VALUE process\_internal\_json(VALUE self, VALUE json, VALUE state,  
VALUE depth, VALUE delim) {  
int first = 1;  
VALUE key\_value\_pairs = rb\_funcall(self, rb\_intern(“to\_a”), 0);

VALUE key\_value = Qnil;  
while((key\_value = rb\_ary\_pop(key\_value\_pairs)) != Qnil) {  
if(first == 1) {  
first = 0;  
}  
else {  
rb\_str\_concat(json, delim);  
}  
VALUE value = rb\_ary\_pop(key\_value);  
VALUE key = rb\_ary\_pop(key\_value);  
generate\_key\_value\_json(self, json, state, depth, key, value);  
}  
}

It seems like there is some optimization in the Hash#each method. I’m  
trying  
to figure out how to get that same performance benefit using C. Perhaps  
its  
is not worth it though.

Does anybody know what going on?

Thank you,  
Brian T.

---

<div class="post-metadata">

**Author:** ![bct](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6a8cbe/32.png) [@bct](https://www.ruby-forum.com/u/bct)\
**Post date:** [October 1, 2006, 7:22am UTC](https://www.ruby-forum.com/t/what-is-the-fastest-way-to-iterate-over-a-hash-in-c/70636/2 "2006-10-01T07:22:21Z")

</div>

I found a better solution in c. This method causes the benchmark to run  
in  
about 1.4 seconds.

static VALUE process\_internal\_json(VALUE self, VALUE json, VALUE state,  
VALUE depth, VALUE delim) {  
VALUE key\_value\_pairs = rb\_funcall(self, rb\_intern(“to\_a”), 0);

VALUE key\_value = Qnil;  
int i;  
for( i = 0; i \< RARRAY(key\_value\_pairs)-\>len; i++) {  
if(i \> 0) {  
rb\_str\_concat(json, delim);  
}  
VALUE key\_value = rb\_ary\_entry(key\_value\_pairs, i);  
VALUE key = rb\_ary\_entry(key\_value, 0);  
VALUE value = rb\_ary\_entry(key\_value, 1);  
generate\_key\_value\_json(self, json, state, depth, key, value);  
}  
}
