Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'benchmark'
- require 'pry'
- require 'pdf-reader'
- require_relative '../lib/format_parser.rb'
- n = 100
- Benchmark.bm(7) { |x|
- x.report("a batch of PDF files - with custom parser as is") {
- Dir["*.pdf"].each do |file|
- n.times {
- begin
- parser = FormatParser::PDFParser.new.call(File.open(file, 'rb'))
- if parser
- parser.page_count
- end
- rescue StandardError
- end
- }
- end
- }
- x.report("a batch of PDF files - with `pdfinfo` binary") {
- Dir["*.pdf"].each do |file|
- n.times {
- info = `pdfinfo #{file} > /dev/null`
- # Get page count
- if match = info.match(/Pages:\s*(\d+)/)
- match.captures[0]
- end
- }
- end
- }
- # This is so slow it hurts
- # x.report("a batch of PDF files - with `imagemagick` binary") {
- # Dir["*.pdf"].each do |file|
- # n.times {
- # page_count = `identify -format %n #{file}`.strip
- # }
- # end
- # }
- x.report("a batch of PDF files - with pdf-reader gem") {
- Dir["*.pdf"].each do |file|
- n.times {
- begin
- info = PDF::Reader.new(File.open(file))
- # Get page count
- if page_count = info.page_count
- page_count
- end
- rescue PDF::Reader::MalformedPDFError
- # -> nil
- end
- }
- end
- }
- }
Add Comment
Please, Sign In to add comment