Ten times faster!
I’ve managed to optimize the performance of my Hac4/GPS data merging script.
Optimized Version
2,33s user 0,06s system 89% cpu 2,677 total
Old merge and libxml-ruby
13,06s user 0,15s system 91% cpu 14,382 total
Old merge and rexml
26,86s user 0,21s system 96% cpu 27,987 total
And this is the track which I used for the tests. The track has 1554 trackpoints. I took this route on Saturday.
New merge algorithm
This should be a comment to a previous post, but it seems that code is not well formatted in comments
This code snippet below should lead me into the right direction for better performance in merging my cycling log data
#
# adjust the 2 arrays
# (works only if gpx starts earlier)
#
while gpx.first[:time] < asc.first[:time]
gpx.shift
end
#
# merge data
#
asc.each do |h|
# next if there gps dropouts
next if h[:time] + 2 > gpx.first[:time]
h.merge!(gpx.shift)
break if gpx.size == 0
end
0
end
Combine Hac4 Data with GPS logger data
If have already a script running which is merging the data of those two devices. But after using libxml I got aware of the awful performance of the merging algorithm.
asc.each do |c|
gpx.each do |e|
f = (c[:time] + @delta.to_i) - e[:time]
if f = -2
d << {:gpx => e, :asc => c}
end
end
end
I created the image which will probably help me in developing a faster algorithm.
Reading GPX with Ruby
Sometime back in May I wrote some utilities to manage the GPX files from my Royaltek RGM 3800. I’ve used rexml to parse the XML file. Now after I’ve read about libxml for ruby I’ve had tried this little gem and I have registered that parsing the GPX file is significantly faster.
This was the old code from May with rexml:
require 'rexml/document'
include REXML
a = Array.new
doc = Document.new File.new(file)
i = 0
doc.elements.each("gpx/trk/trkseg/trkpt") { |e|
a << {:time => Time.parse(e.elements["time"].to_a.first.to_s)}
a[i][:lat] = e.attributes["lat"].to_s;
a[i][:lon] = e.attributes["lon"].to_s;
a[i][:ele] = e.elements["ele"].to_a.first.to_s
a[i][:speed] = e.elements["speed"].to_a.first.to_s
a[i][:course] = e.elements["course"].to_a.first.to_s
a[i][:fix] = e.elements["fix"].to_a.first.to_s
i += 1
}
return a
end
And this is the code using libxml which I wrote after learning a little bit more ruby
require 'rubygems'
require 'libxml'
a = Array.new
doc = LibXML::XML::Document.file(file)
nodes = doc.find '/ns:gpx/ns:trk/ns:trkseg/ns:trkpt',
"ns:http://www.topografix.com/GPX/1/0"
nodes.each do |e|
a << {:lat => e[:lat], :lon => e[:lon]}
node = a.last
e.each do |n|
node[n.name.to_sym] = n.children.to_s if n.children?
end
node[:time] = Time.parse(node[:time])
end
return a
end
This code is part of a little script which I use to combine data from the GPS logger device with the data which is provided by my cycle computer (heart rate, barometric height, cadence, speed etc.).


