在Ruby(或Rails)中,比较两个大型字符串是否更快,或者在使用uniq之前将其转换为Fixnum?

时间:2016-10-04 00:54:17

标签: ruby-on-rails ruby

给定2000个<html> <head> <title>Image carousel</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" type="text/css" href="reset.css"> <link rel="stylesheet" type="text/css" href="carouselcss.css"> <!-- <link href="https://fonts.googleapis.com/css?family=Baloo+Tamma" rel="stylesheet"> --> </head> <body> <header> header is here </header> <div id="carousel"> <div class="sliderbuttons"> <input type="button" name="next" id="next" value="&gt;"> <input type="button" name="prev" id="prev" value="&lt;"> </div> <div class="slides"> <img src="http://www.planwallpaper.com/static/images/4-Nature-Wallpapers-2014-1_ukaavUI.jpg" alt="image1" class="slide active"> <img src="http://www.mrwallpaper.com/wallpapers/green-Rice-1600x900.jpg" alt="image2" class="slide"> <img src="http://cdn.wonderfulengineering.com/wp-content/uploads/2016/01/nature-wallpapers-10.jpg" alt="image3" class="slide"> </div> <div class="indicators"> <div class="circle blip"></div> <div class="circle"></div> <div class="circle"></div> </div> </div> </div> </body> </html> html,body { height: 100%; position: relative; background-color: #ccc; } *{ box-sizing: border-box; } #container { width: 90%; margin: 0 auto; height: 100%; background-color: white; } header { background: black; height: 20px; padding: 1.5em; color: white; } #carousel { position: relative; margin: 0 auto; width: 45%; margin-top: 15px; height: 100%; background-color: pink } .slide { position: absolute; width: 100%; z-index: 0; height: 100%; } .sliderbuttons { } #prev,#next { position: absolute; background-color: rgba(255, 148, 41, 0.68); box-shadow: 2px white; border:none; font-size: 2em; padding: 1%; color: white; font-weight: bold; font-family: 'Baloo Tamma', cursive; height: 100%; width: 10%; /*making the prev,next on top of content*/ z-index: 2; } #prev { left:0; } #next { right:0; } .active { z-index: 1; } .indicators { z-index: 2; position: absolute; bottom:49%; left: 45%; } .circle { border-radius: 10px; width: 10px; height: 5px; border:2px solid black; } .indicators div { padding: 8px; margin: 2px; display: inline-block; } .blip { background-color: orange; } div.indicators:hover { cursor: pointer; } 个对象,每个对象的Story属性等于大约500个字符的字符串。

比较它们的最快方法是什么?

  1. 这样做:body
  2. OR

    1. 首先将每个@stories.uniq { |story| story.body }转换为body表示形式,然后运行Fixnum
    2. 我有一种模糊的感觉,即计算机能够比字符更快地比较数字,但我也知道每个字符实际上只是用字节表示。

1 个答案:

答案 0 :(得分:4)

很容易对这些事情进行基准测试。例如:

require 'benchmark'

string_array = 1.upto(2000).inject([]) do |arr| 
  arr << 1.upto(500).inject("") { |str| str << rand(10).to_s } 
end

fixnum_array = string_array.map(&:to_i)

Benchmark.bm do |x|
  x.report("bignum:") { 1000.times { fixnum_array.uniq } }
  x.report("bignum_and_to_i:") { 1000.times { string_array.map(&:to_i).uniq } }
  x.report("string:") { 1000.times { string_array.uniq } }
end

输出:

                        user     system      total        real
bignum:             1.710000   0.010000   1.720000 (  1.729463)
bignum_and_to_i:   28.500000   0.160000  28.660000 ( 28.738891)
string:             1.740000   0.000000   1.740000 (  1.754165)

比较包含大约500位数字的2000个字符串比首先将字符串转换为数字然后进行比较要快得多。

比较长字符串与比较大字符串不会产生很大的不同。

结论:将长字符串转换为大字符串是如此之慢,以至于只比较字符串会更快。