Guest User

Untitled

a guest
Jun 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. require 'benchmark'
  2. require 'pry'
  3. require 'pdf-reader'
  4. require_relative '../lib/format_parser.rb'
  5.  
  6. n = 100
  7. Benchmark.bm(7) { |x|
  8. x.report("a batch of PDF files - with custom parser as is") {
  9. Dir["*.pdf"].each do |file|
  10. n.times {
  11. begin
  12. parser = FormatParser::PDFParser.new.call(File.open(file, 'rb'))
  13.  
  14. if parser
  15. parser.page_count
  16. end
  17. rescue StandardError
  18. end
  19. }
  20. end
  21. }
  22.  
  23. x.report("a batch of PDF files - with `pdfinfo` binary") {
  24. Dir["*.pdf"].each do |file|
  25. n.times {
  26. info = `pdfinfo #{file} > /dev/null`
  27.  
  28. # Get page count
  29. if match = info.match(/Pages:\s*(\d+)/)
  30. match.captures[0]
  31. end
  32. }
  33. end
  34. }
  35.  
  36. # This is so slow it hurts
  37. # x.report("a batch of PDF files - with `imagemagick` binary") {
  38. # Dir["*.pdf"].each do |file|
  39. # n.times {
  40. # page_count = `identify -format %n #{file}`.strip
  41. # }
  42. # end
  43. # }
  44.  
  45. x.report("a batch of PDF files - with pdf-reader gem") {
  46. Dir["*.pdf"].each do |file|
  47. n.times {
  48. begin
  49. info = PDF::Reader.new(File.open(file))
  50.  
  51. # Get page count
  52. if page_count = info.page_count
  53. page_count
  54. end
  55. rescue PDF::Reader::MalformedPDFError
  56. # -> nil
  57. end
  58. }
  59. end
  60. }
  61. }
Add Comment
Please, Sign In to add comment