Project

General

Profile

Actions

Bug #6509

closed

String#gsub is too slow if receiver includes a binary

Added by okkez (okkez _) almost 12 years ago. Updated almost 12 years ago.

Status:
Closed
Target version:
ruby -v:
ruby 2.0.0dev (2012-05-28 trunk 35830) [x86_64-linux]
Backport:
[ruby-dev:45688]

Description

=begin

以下のようなコードで String#gsub が遅くなります。

  • b = "" の場合(A): 0.2840230464935303
  • b = "\xB9" の場合(B): 4.183771848678589

-- coding: utf-8 --

a = ("abcde\n"*50000).force_encoding("binary")
#b = ""
b = "\xB9".force_encoding("binary")
c = ("efghi\n"*50000).force_encoding("binary")

d = "#{a}#{b}#{c}"

start = Time.now.to_f
d.gsub(/\n/) { "" }
puts(Time.now.to_f - start)

それぞれの場合で、プロファイルを取ってみたので添付します。

(B)の場合に、search_nonascii を約20万回呼び出して処理時間の92%を費しています。
(A)の場合は、約10万回しか呼び出しておらず、処理時間も短いです。

=end


Files

callgrind.out.9937 (521 KB) callgrind.out.9937 (A)の場合 okkez (okkez _), 05/29/2012 10:03 AM
callgrind.out.10091 (521 KB) callgrind.out.10091 (B)の場合 okkez (okkez _), 05/29/2012 10:03 AM
Actions

Also available in: Atom PDF

Like0
Like0Like0