我的代码在这里
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(' ')}
Run Code Online (Sandbox Code Playgroud)
如果我有一个像大字符串这样的大字符串,那么这个过程需要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"]
Run Code Online (Sandbox Code Playgroud)
是否有可能采用其他有效方式
使用扫描而不是分割,您可以直接获得单词对.
s.scan(/\S+(?:\s+\S+)?/)
Run Code Online (Sandbox Code Playgroud)
编辑:只是为了向自己保证这是相对有效的,我做了一点微观基准测试.以下是迄今为止所见答案的结果:
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)
Run Code Online (Sandbox Code Playgroud)