Mi è stato chiesto di documentare le mie osservazioni sulle performance della formattazione di oggetti di tipo Date, DateTime, Time.
A seguito una serie di test di verifica delle prestazioni.Questi test sono stati eseguiti su un DualCore T9500 (2.6ghz) 4 GB ram sistema operativo Ubuntu 8.10 intrepid.
Questo il codice usato per eseguire i test:
require ‘ruby-prof’
MAX = 50000
class CodeProfiler
def self.time_this(comment, &block)
RubyProf.measure_mode = RubyProf::PROCESS_TIME
RubyProf.start
yield
result = RubyProf.stop
puts “\nTimings for #{comment}”
printer = RubyProf::FlatPrinter.new(result)
printer.print(STDOUT, 0)
end
end
CodeProfiler.time_this(“Class Date -> strftime(\”%d/%m/%Y\”):”) {
x = ”
t = Date.new()
(0..MAX).each do |i|
x = t.strftime(“%d/%m/%Y”)
end
}
CodeProfiler.time_this(‘Class Date -> #{t.day}/#{t.month}/#{t.year}:’) {
x = ”
t = Date.new()
(0..MAX).each do |i|
x = “#{t.day}/#{t.month}/#{t.year}”
end
}
CodeProfiler.time_this(“Class DateTime -> strftime(\”%d/%m/%Y %H:%M\”):”) {
x = ”
t = DateTime.new()
(0..MAX).each do |i|
x = t.strftime(“%d/%m/%Y %H:%M”)
end
}
CodeProfiler.time_this(‘Class DateTime -> #{t.day}/#{t.month}/#{t.year} #{t.hour}:#{t.min}:’) {
x = ”
t = DateTime.new()
(0..MAX).each do |i|
x = “#{t.day}/#{t.month}/#{t.year} #{t.hour}:#{t.min}”
end
}
CodeProfiler.time_this(“Class Time -> strftime(\”%d/%m/%Y %H:%M\”):”) {
x = ”
t = Time.new()
(0..MAX).each do |i|
x = t.strftime(“%d/%m/%Y %H:%M”)
end
}
CodeProfiler.time_this(‘Class Time -> #{t.day}/#{t.month}/#{t.year} #{t.hour}:#{t.min}:’) {
x = ”
t = Time.new()
(0..MAX).each do |i|
x = “#{t.day}/#{t.month}/#{t.year} #{t.hour}:#{t.min}”
end
}
Risultati
Timings for Class Date -> strftime(“%d/%m/%Y”):
Thread ID: 70286299744000
Total: 14.590000
Timings for Class Date -> #{t.day}/#{t.month}/#{t.year}:
Thread ID: 70286299744000
Total: 0.880000
Timings for Class DateTime -> strftime(“%d/%m/%Y %H:%M”):
Thread ID: 70286299744000
Total: 22.800000
Timings for Class DateTime -> #{t.day}/#{t.month}/#{t.year} #{t.hour}:#{t.min}:
Thread ID: 70286299744000
Total: 1.500000
Timings for Class Time -> strftime(“%d/%m/%Y %H:%M”):
Thread ID: 70286299744000
Total: 0.190000
Timings for Class Time -> #{t.day}/#{t.month}/#{t.year} #{t.hour}:#{t.min}:
Thread ID: 70286299744000
Total: 0.550000
Conclusioni
Da questi test si può ricavare come solamente la classe time sia performante nell’uso del metodo strftime, sconsiglio quindi l’uso di questo metodo su oggetti di tipo Date e DateTime.
(in allegato il file test.rb e log.txt)









Bel lavoro! Ho rieseguito i test sul mio pc, che è anche lui un Ubuntu 8.10 ma con un Core Duo 2 T7200 come CPU (quindi 0.6 GHz in meno), usando però anche la gemma date-performance, che si installa con
gem install date-performance date-performance –source=http://tomayko.com
e si usa con require “date/performance”
Come vedrete nel sommario qui sotto i primi due totali (N1 e N2) si riferiscono a due test fatti senza date-performance, i secondi due (D1 e D2) invece sono di due test che la usano. Notate come DateTime.strftime sia sempre pessima mentre Date.strftime diventi due volte più veloce della conversione manuale, da 20 volte più lenta che era.
Timings for Class Date -> strftime(“%d/%m/%Y”):
(N1) Total: 26.630000
(N2) Total: 26.370000
(D1) Total: 0.440000
(D2) Total: 0.400000
Timings for Class Date -> #{t.day}/#{t.month}/#{t.year}:
(N1) Total: 1.350000
(N2) Total: 1.350000
(D1) Total: 0.970000
(D2) Total: 0.960000
Timings for Class DateTime -> strftime(“%d/%m/%Y %H:%M”):
(N1) Total: 40.960000
(N2) Total: 40.880000
(D1) Total: 39.840000
(D2) Total: 40.180000
Timings for Class DateTime -> #{t.day}/#{t.month}/#{t.year} #{t.hour}:#{t.min}:
(N1) Total: 2.330000
(N2) Total: 2.300000
(D1) Total: 1.890000
(D2) Total: 1.880000
Timings for Class Time -> strftime(“%d/%m/%Y %H:%M”):
(N1) Total: 0.280000
(N2) Total: 0.280000
(D1) Total: 0.290000
(D2) Total: 0.290000
Timings for Class Time -> #{t.day}/#{t.month}/#{t.year} #{t.hour}:#{t.min}:
(N1) Total: 0.820000
(N2) Total: 0.830000
(D1) Total: 0.810000
(D2) Total: 0.800000
Conclusione: tenersi alla larga da DateTime, e al suo posto usare Time oppure Date con la gemma date-performance.
Ho eseguito il test anche da me aumentando il numero di cicli a 900000 e la differenza diventa quasi 3 volte, direi che la libreria è fondamentale
Timings for Class Date -> strftime(“%d/%m/%Y”):
Thread ID: 69969470861060
Total: 4.610000
Timings for Class Date -> #{t.day}/#{t.month}/#{t.year}:
Thread ID: 69969470861060
Total: 11.170000
Timings for Class Time -> strftime(“%d/%m/%Y %H:%M”):
Thread ID: 69969470861060
Total: 3.080000
Timings for Class Time -> #{t.day}/#{t.month}/#{t.year} #{t.hour}:#{t.min}:
Thread ID: 69969470861060
Total: 9.430000