简体   繁体   中英

How do I simplify pushing multiple values into an array in Ruby?

How would you improve this:

time = Time.now
    @time = []
    @time.push(
               (time-1.week).strftime("%m-%d"),
               (time-6.days).strftime("%m-%d"),
               (time-5.days).strftime("%m-%d"),
               (time-4.days).strftime("%m-%d"),
               (time-3.days).strftime("%m-%d"),
               (time-2.days).strftime("%m-%d"),
               (time-1.day).strftime("%m-%d"),
               (time).strftime("%m-%d")
              )

I'm trying out some of the suggestions below:

time = Time.now
    iterations = 1000
    Benchmark.bm do |bm|
      bm.report do
         iterations.times do
            @time = 7.downto(0).map { |v| (time - v.days).strftime("%m-%d") }
          end
      end
      bm.report do
       iterations.times do
          @time = []
          @time.push(
               (time-1.week).strftime("%m-%d"),
               (time-6.days).strftime("%m-%d"),
               (time-5.days).strftime("%m-%d"),
               (time-4.days).strftime("%m-%d"),
               (time-3.days).strftime("%m-%d"),
               (time-2.days).strftime("%m-%d"),
               (time-1.day).strftime("%m-%d"),
               (time).strftime("%m-%d")
              )
       end
      end
     end

       user     system      total        real
   0.350000   0.960000   1.310000 (  1.310054)
   0.310000   0.840000   1.150000 (  1.156484)

downto is demonstrably slower than my method.

The next test used the method:

@time = (0..7).map { |x| (time - x.days).strftime("%m-%d") }.reverse

1000 iterations

       user     system      total        real
   0.340000   0.980000   1.320000 (  1.321518)
   0.300000   0.840000   1.140000 (  1.149759)

5000 iterations

       user     system      total        real
   1.720000   4.800000   6.520000 (  6.545335)
   1.530000   4.180000   5.710000 (  5.712035)

I'm having a hard time wrapping my head around this without looking at both downto and map in the Ruby core, but in both cases my more elongated method of writing this responds faster than the more simplified methods (I like the answers below much more from a readability standpoint). Please shed some light on my tests if I'm doing it wrong. I expected map to blow my way out of the water.

UPDATED FOR STEFAN'S ANSWER

So I see Stefan's answer below and throw it in the tester:

       user     system      total        real
   0.040000   0.000000   0.040000 (  0.035976)
   1.520000   4.180000   5.700000 (  5.704401)

Holy crap! 5000 iterations and it absolutely destroys my method.

Because he accurately points out that I'm only interested in Dates, I decide to change my own method from Time.now to Date.today and test it:

       user     system      total        real
   0.090000   0.000000   0.090000 (  0.085940)
   0.390000   0.000000   0.390000 (  0.398143)

It's somewhat weird that in the first test Stefan's method clocks in at 0.0359 and in the second clocks at 0.0859, more than double, but it's still hundredths of a second over 5000 iterations - so I think I'm deep into splitting hairs territory here.

Nevertheless - Stefan's way obliterates my own method - so I'm giving him the check mark.

You can do as below using #downto -

time = Time.now
@time = 7.downto(0).map {|v| (time - v.days).strftime("%m-%d") }

You can use not pushing itself, but collection, ie combination of Range from zero to seven, and Array 's #map method:

time = Time.now
@time = (0..7).map {|v| (time - v.days).strftime("%m-%d") }.reverse
# => ["04-24", "04-25", "04-26", "04-27", "04-28", "04-29", "04-30", "05-01"]

I would write it using the Array#map method

time = Time.now
@time = (0..7).map { |x| (time - x.days).strftime("%m-%d") }.reverse

Since you're only interested in dates, you could use a Range of Date instances (dates increment in 1-day steps ):

today = Date.today
(today-7..today).map { |date| date.strftime("%m-%d") }
#=> ["04-24", "04-25", "04-26", "04-27", "04-28", "04-29", "04-30", "05-01"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM