require'pygments'require'benchmark'includeBenchmark# number of iterationsnum=20code='app = angular.module("app", [])'puts"Benchmarking....\n"puts"Iterations: "+num.to_s+"\n"defhighlight_fail_raise_exception(code,language)Pygments.highlight(code,:lexer=>language)rescue=>ePygments.highlight(code,:lexer=>"text")enddefhighlight_fail_dont_raise_exception(code,language)ifPygments::Lexer.find(language)Pygments.highlight(code,:lexer=>language)elsePygments.highlight(code,:lexer=>"text")endendBenchmark.bm(40)do|x|x.report("Currect language before fixed")doforiin1..numhighlight_fail_raise_exception(code,"javascript")endendendBenchmark.bm(40)do|x|x.report("Wrong language before fixed")doforiin1..numhighlight_fail_raise_exception(code,"app/assets/javascripts/angular/app.js")endendendBenchmark.bm(40)do|x|x.report("Currect language after fixed")doforiin1..numhighlight_fail_dont_raise_exception(code,"javascript")endendendBenchmark.bm(40)do|x|x.report("Wrong language after fixed")doforiin1..numhighlight_fail_dont_raise_exception(code,"app/assets/javascripts/angular/app.js")endendend
可以看到差異是非常大的:
12345678910
Benchmarking....
Iterations: 20
user system total real
Currect language before fixed 0.000000 0.000000 0.000000 ( 0.190763)
user system total real
Wrong language before fixed 0.020000 0.010000 2.740000 ( 2.730492)
user system total real
Currect language after fixed 0.020000 0.000000 0.020000 ( 0.074654)
user system total real
Wrong language after fixed 0.000000 0.010000 0.010000 ( 0.018810)