File: bench.rb

package info (click to toggle)
ruby-pygments.rb 2.3.0%2Bds-2.2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 300 kB
  • sloc: ruby: 547; python: 139; makefile: 10; sh: 4
file content (28 lines) | stat: -rw-r--r-- 845 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# frozen_string_literal: true

require File.join(File.dirname(__FILE__), '/lib/pygments.rb')
require 'benchmark'

# number of iterations
num = ARGV[0] ? ARGV[0].to_i : 10

# we can also repeat the code itself
repeats = ARGV[1] ? ARGV[1].to_i : 1

code = File.open('test/test_pygments.rb').read.to_s * repeats

puts "Benchmarking....\n"
puts "Size: #{code.bytesize} bytes\n"
puts "Iterations: #{num}\n"

Benchmark.bm(40) do |x|
  x.report('pygments popen                             ') do
    (1..num).each { |_i|; Pygments.highlight(code, lexer: 'python'); }
  end
  x.report('pygments popen (process already started)   ') do
    (1..num).each { |_i|; Pygments.highlight(code, lexer: 'python'); }
  end
  x.report('pygments popen (process already started 2) ') do
    (1..num).each { |_i|; Pygments.highlight(code, lexer: 'python'); }
  end
end