我做了Ruby的陣列concat()
vs +
操作的小性能測試,concat()
太快了。Ruby Array concat與+速度?
但是我不清楚爲什麼concat()
是如此之快?
任何人都可以幫忙嗎?
這是我使用的代碼:
t = Time.now
ar = []
for i in 1..10000
ar = ar + [4,5]
end
puts "Time for + " + (Time.now - t).to_s
t = Time.now
ar = []
for i in 1..10000
ar.concat([4,5])
end
puts "Time for concat " + (Time.now - t).to_s
僅供參考:) http://www.ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc /Benchmark.html –
http://www.joelonsoftware.com/articles/fog0000000319.html – Fuser97381