On Thu, Feb 20, 2003 at 07:53:14AM +0900, Travis Whitton wrote:
> # This is ran about a million times
> h = Hash.new(0)
> data.scan(iptok).each do |tok|
>   h[tok] += 1
> end
> 
> At first, I though I could do something like this:
> 
> # This is ran about a million times
> h = Hash.new(0)
> data.scan(iptok).each do |tok|
>   h[tok].succ
> end
> 
> But I realized that it doesn't modify the final value; however, I did notice
> that it ran twice as fast than the former example.

Unfortunately it doesn't actually put anything into the hash either, so it's
not a fair comparison. Have you tried:

   h[tok] = 1

and see how that performs? Also

   h[tok] = h[tok]

The first case will show time for inserting elements into hash, and the
second case both retrieving and inserting elements (they will be inserted
with value 'nil'). I suspect that the '+1' isn't actually going to be the
main source of the problem.

> is there
> any way to do the first example using Inline::C or something similar?

See 'extending Ruby' in the Pickaxe.

Regards,

Brian.