Patch #20369

Use String#casecmp for case insensitive comparison

Added by Go MAEDA over 3 years ago. Updated over 3 years ago.

Status:ClosedStart date:
Priority:NormalDue date:
Assignee:-% Done:

0%

Category:Code cleanup/refactoring
Target version:3.2.0

Description

String#casecmp is faster than String#downcase. We can save some CPU cycles by replacing String#downcase with String#casecmp.

The following is a script to benchmark.

#!/usr/bin/env ruby

def measure(p)
  t1 = Time.now
  1000000.times {p.call}
  t2 = Time.now
  t2 - t1
end

str1 = (1..100).map {('A'..'z').to_a.sample}.join
str2 = str1.downcase

puts "String#casecmp  :  #{measure(->{ str1.casecmp(str2) == 0})} sec" 
puts "String#downcase :  #{measure(->{ str1.downcase == str2 })} sec" 

Here is a result. casecmp is remarkably faster than downcase.

laphroaig:patches maeda$ ruby casecmp-vs-downcase.rb
String#casecmp  :  0.358929 sec
String#downcase :  0.557218 sec

replace-downcase-with-casecmp.diff Magnifier (4.57 KB) Go MAEDA, 2015-07-19 06:36


Related issues

Related to Redmine - Defect #20278: Wrong syntax for resizing inline images will throw a 500 ... Closed
Related to Redmine - Defect #27780: Attachment sort doesn't work with Unicode New

Associated revisions

Revision 14484
Added by Toshi MARUYAMA over 3 years ago

use String#casecmp for case insensitive comparison (#20369)

Contributed by Go MAEDA.

Revision 14498
Added by Jean-Philippe Lang over 3 years ago

#downcase no longer needed after r14484 (#20369).

Revision 14502
Added by Jean-Philippe Lang over 3 years ago

#downcase no longer needed after r14484 (#20369).

Revision 14676
Added by Jean-Philippe Lang about 3 years ago

Fixed unsafe call to #casecmp (#20369, #21000).

History

#1 Updated by Toshi MARUYAMA over 3 years ago

  • Target version set to 3.2.0

#2 Updated by Go MAEDA over 3 years ago

  • Related to Defect #20278: Wrong syntax for resizing inline images will throw a 500 error added

#3 Updated by Toshi MARUYAMA over 3 years ago

  • Status changed from New to Closed

Committed in trunk r14484, thanks.

#4 Updated by Go MAEDA about 1 year ago

  • Related to Defect #27780: Attachment sort doesn't work with Unicode added

Also available in: Atom PDF