I recently had to write a quick script to update a text file full of pdf annotations. I wrote the script in Ruby then I wondered how much of a difference in speed I would get if I turned it into Crystal. See below.
Last active
March 19, 2018 13:26
-
-
Save lodestone/d11b4651b6d607f61909d621754da77c to your computer and use it in GitHub Desktop.
A Small Ruby vs Crystal Comparison
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@AmarasingamReligionNewAtheism2012 224 | |
Annotation Summary of Eclipse_Phase_Transhumanitys_Fate 2.pdf. | |
Credits | |
Highlight [1]: The Posthumans dedicate this book to Jef Smith, our companion of many days & nights around the table. Jef was a tireless organizer in Chicago's science-fiction/ fantasy fandom, including the Think Galactic reading group and spin-off convention, Think Galacticon. In 2015, he co-published the Sisters of the Revolution: A Feminist Speculative Fiction Anthology with PM Press. We will miss Jef's encouragement, wit, and bottomless generosity. The person who lives large in the lives of their friends is not soon forgotten. (@AmarasingamReligionNewAtheism2012 225) | |
and Note: This is the dedication | |
The Fall | |
Highlight [4]: In Transhumanity's Fate, you play secret agents protecting the scattered remnants of transhumanity from threats that could wipe it out once and for all. (@AmarasingamReligionNewAtheism2012 228) | |
Highlight [4]: The Fall became a race to get key people, equipment, and resources off Earth as quickly as possible—and damn the cost to everyone else. (@AmarasingamReligionNewAtheism2012 228) | |
Note [4]: This is a Purple Note. (@AmarasingamReligionNewAtheism2012 228) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@AmarasingamReligionNewAtheism2012 224 | |
Annotation Summary of Eclipse_Phase_Transhumanitys_Fate 2.pdf. | |
Credits | |
Highlight [1]: The Posthumans dedicate this book to Jef Smith, our companion of many days & nights around the table. Jef was a tireless organizer in Chicago's science-fiction/ fantasy fandom, including the Think Galactic reading group and spin-off convention, Think Galacticon. In 2015, he co-published the Sisters of the Revolution: A Feminist Speculative Fiction Anthology with PM Press. We will miss Jef's encouragement, wit, and bottomless generosity. The person who lives large in the lives of their friends is not soon forgotten. | |
and Note: This is the dedication | |
The Fall | |
Highlight [4]: In Transhumanity's Fate, you play secret agents protecting the scattered remnants of transhumanity from threats that could wipe it out once and for all. | |
Highlight [4]: The Fall became a race to get key people, equipment, and resources off Earth as quickly as possible—and damn the cost to everyone else. | |
Note [4]: This is a Purple Note. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
file = ARGV.first | |
new_file = [] of String | |
citekey = "" | |
starting_page_number = 0 | |
current_page = 0 | |
if File.exists?(file) | |
text = File.read(file) | |
if text.lines.first =~ /@.*\d+/ | |
header = text.lines.first.strip.split(" ") | |
citekey = header.first | |
num = header.last | |
starting_page_number = num.to_i | |
end | |
text.lines.each do |line| | |
if line =~ /^(Highlight|Note)\s\[\d+\]:/ | |
match_data = line.match(/^(Highlight|Note)\s\[(?<page>\d+)\]/) | |
page_number = match_data["page"].to_i if match_data | |
current_page = starting_page_number + page_number if page_number | |
new_file << "#{line.gsub("\n", "")} (#{citekey} #{current_page})" | |
else | |
new_file << line | |
end | |
end | |
end | |
puts new_file.join("\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
file = ARGV.first | |
new_file = [] | |
citekey = "" | |
starting_page_number = 0 | |
current_page = 0 | |
if File.exists?(file) | |
text = File.read(file) | |
if text.lines.first =~ /@.*\d+/ | |
header = text.lines.first.strip.split(" ") | |
citekey = header.first | |
num = header.last | |
starting_page_number = num.to_i | |
end | |
text.lines.each do |line| | |
if line =~ /^(Highlight|Note)\s\[\d+\]:/ | |
match_data = line.match(/^(Highlight|Note)\s\[(?<page>\d+)\]/) | |
page_number = match_data["page"].to_i if match_data | |
current_page = starting_page_number + page_number if page_number | |
new_file << "#{line.gsub("\n", "")} (#{citekey} #{current_page})" | |
else | |
new_file << line | |
end | |
end | |
end | |
puts new_file.join("") |
# Ruby
time ./fix-annotations.rb annotations.txt > annotations-fixed.txt
./fix-annotations.rb annotations.txt > annotations-fixed.txt 0.14s user 0.10s system 68% cpu 0.346 total
# Crystal
time ./fix-annotations annotations.txt > annotations-fixed.txt
./fix-annotations annotations.txt > annotations-fixed.txt 0.00s user 0.00s system 55% cpu 0.013 total
- Ruby:
0.14s
- Crystal:
0.00s
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment