title: Why is string comparison so fast compared to integer comparison?
body: <p>In a Ruby project that I have been spending some time on lately, I have been counting the intersection of two large sets of strings. </p>
  
  <p>From what I thought I understood, I decided that it would make a lot of sense to compare integers instead of strings (all of these strings are being held in a database, and i could easily just swap them out for ids)</p>
  
  <p>When i actually did the benchmarking, i ended up finding the complete opposite.</p>
  
  <p>First i generated sets of 850 strings, and sets of ~850 large integers:</p>
  
  <pre><code>r = Random.new
  w1 = (1..850).collect{|i| w="";(0..3).collect{|j| (rand*26 + 10).to_i.to_s(35)}.each{|l| w+=(l.to_s)};w}.to_set
  w2 = (1..850).collect{|i| w="";(0..3).collect{|j| (rand*26 + 10).to_i.to_s(35)}.each{|l| w+=(l.to_s)};w}.to_set
  
  i1 = (1..2000).collect{|i| (r.rand*1000).to_i**2}.to_set;
  i2 = (1..2000).collect{|i| (r.rand*1000).to_i**2}.to_set;
  </code></pre>
  
  <p>And then i timed the comparisons:</p>
  
  <pre><code>t=Time.now;(0..1000).each {|i| w1 &amp; w2};Time.now-t
  =&gt; 0.301727
  t=Time.now;(0..1000).each {|i| i1 &amp; i2};Time.now-t
  =&gt; 0.70151
  </code></pre>
  
  <p>Which i thought was crazy! I always thought integer comparison was much faster..</p>
  
  <p>So i was wondering if anybody in the world of stacks knew anything about why the string comparison is so much faster in ruby, i would really appreciate hearing your thoughts.</p>