2013-03-22 87 views
1

我的代碼是在這裏將一個字符串分解成有效的方式部分

str = "Early in his first term in office, Obama signed into law economic stimulus legislation in response" 
arr= str.split(" ") 
set_element= arr.each_cons(2).to_a 
sub_str = set_element.map {|i| i.join(' ')} 

,如果我有一個大的串像非常大的字符串,然後這個過程需要6.50秒 因爲我想這種結果的

sub_str= ["Early in", "in his", "his first", "first term", "term in", "in office,", "office, Obama", "Obama signed", "signed into", "into law", "law economic", "economic stimulus", "stimulus legislation", "legislation in", "in response"] 

是否有可能與任何有效的方式另闢蹊徑

回答

7

使用掃描,而不是拆分,你可以直接得到你的單詞對。

s.scan(/\S+(?:\s+\S+)?/) 

編輯:只是爲了保證自己,這是相對高效的,我做了a little micro-benchmark。以下是迄今爲止所見的結果:

ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-linux] 
10 times on string of size 2284879 
       user  system  total  real 
original  4.180000 0.070000 4.250000 ( 4.272856) 
sergio  2.090000 0.000000 2.090000 ( 2.102469) 
dbenhur  1.050000 0.000000 1.050000 ( 1.042167) 
+0

真棒我對此沒有詞 – 2013-03-22 06:16:28

1
set_element = arr.each_cons(2).to_a 

線之上創建噸你不需要的臨時對象。嘗試這個,應該更快:

str = "Early in his first term in office, Obama signed into law economic stimulus legislation in response" 
arr = str.split(" ") 
sub_str = arr.each_with_object([]).with_index do |(el, memo), idx| 
    if idx % 2 == 0 
    memo << el 
    else 
    memo.last << ' ' << el 
    end 

end 

sub_str # => ["Early in", "his first", "term in", "office, Obama", "signed into", "law economic", "stimulus legislation", "in response"] 
0

您可以試試這個。一步少:) :)

arr= str.scan(/\S+/) 
s = [] 
arr.each_with_index { |x, i| s << (x + " " + arr[i + 1]) if arr[i+1] } 
相關問題