Skip to content

Instantly share code, notes, and snippets.

@DanPatey
Created September 11, 2015 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanPatey/8242c5b971429a37cab2 to your computer and use it in GitHub Desktop.
Save DanPatey/8242c5b971429a37cab2 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'rubygems'
require 'json'
require 'pp'
# Read in our JSON file and convert to Hash so we can play with it
json = File.read('scan-times.json')
json_hash = JSON.parse(json, :symbolize_names => true)
# Create variables for our conditionals
$i = 0
shortest_time = 0
latest_time = 0
# Update this to run to the end of the file
begin
start_time = json_hash[$i][:start].split(' ').last
start_time = Time.parse(start_time)
end_time = json_hash[$i][:stop].split(' ').last
end_time = Time.parse(end_time)
current_time = end_time - start_time
# Keep a running total for our average time
total_time +=current_time
$i +=1
# Find the shortest time
if current_time < shortest_time |do|
shortest_time = current_time
return shortest_time
# Find the longest time
elsif current_time > latest_time |do|
latest_time = current_time
return latest_time
end
return total_time, $i
end while $i > json_hash
average_time = total_time / $i
print 'The average scan time was ' + average_time
print 'The fastest scan was ' + shortest_time
print 'The longest scan was ' + latest_time
print 'the total time to calculate was ' + total_time
@baweaver
Copy link

Cleaner

#!/usr/bin/ruby

require 'json'

# Read in our JSON file and convert to Hash so we can play with it
json      = File.read('scan-times.json')
json_hash = JSON.parse(json, symbolize_names: true)

times = json_hash.map { |obj|
  Time.parse(obj[:end].split.last) - Time.parse(obj[:start].split.last)
}

min_time, max_time = times.minmax { |a,b| a <=> b }
total_time         = times.reduce(:+)
average_time       = total_time.to_f / times.size.to_f

puts "The average scan time was #{average_time}"
puts "The fastest scan was #{min_time}"
puts "The longest scan was #{max_time}"
puts "the total time to calculate was #{total_time}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment