Guest User

Untitled

a guest
Jun 21st, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. require "rubygems"
  2. require "json"
  3.  
  4. class Stat
  5. attr_reader :times
  6.  
  7. def initialize(times)
  8. @times = times
  9. end
  10.  
  11. def mean
  12. @mean ||= times.inject(0) { |sum, t| sum + t } / size
  13. end
  14.  
  15. def size
  16. @size ||= times.size
  17. end
  18.  
  19. def sorted
  20. @sorted ||= times.sort
  21. end
  22.  
  23. def median
  24. sorted[size / 2]
  25. end
  26.  
  27. def max
  28. sorted.last
  29. end
  30.  
  31. def min
  32. sorted.first
  33. end
  34.  
  35. def stddev
  36. Math.sqrt(times.inject(0) { |sum, t| sum + (mean - t) ** 2 } / size)
  37. end
  38.  
  39. def inspect
  40. {
  41. :call_count => size,
  42. :mean => print_float(mean),
  43. :median => print_float(median),
  44. :stddev => print_float(stddev),
  45. :max => print_float(max),
  46. :min => print_float(min)
  47. }.inspect
  48. end
  49.  
  50. def print_float(float)
  51. ("%.5f" % float.to_f).to_f
  52. end
  53.  
  54. end
  55.  
  56.  
  57. firefox = JSON.parse(File.read("firefox.json"))
  58. android = JSON.parse(File.read("android.json"))
  59.  
  60. commands = (firefox.keys + android.keys).uniq.sort
  61.  
  62. commands.each do |command|
  63. puts "#{command}: \n"
  64. if ff_times = firefox[command]
  65. puts "\tfirefox : #{Stat.new(ff_times).inspect}"
  66. else
  67. puts "\tfirefox : not called"
  68. end
  69.  
  70. if a_times = android[command]
  71. puts "\tandroid : #{Stat.new(a_times).inspect}"
  72. else
  73. puts "\tandroid : not called"
  74. end
  75.  
  76. end
Add Comment
Please, Sign In to add comment