ruby 基准删除超过20秒的文件(目标:jRuby 1.7.4 Ubuntu 14.04)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 基准删除超过20秒的文件(目标:jRuby 1.7.4 Ubuntu 14.04)相关的知识,希望对你有一定的参考价值。

require 'benchmark/ips'
require 'fileutils'

DIR = '/opt/tmp'

def eachfile
  now = Time.now
  Dir.glob("#{DIR}/puma*").each do |f|
    FileUtils.rm_f f if now - File.mtime(f) > 20
  end
end

def onecommand
  now = Time.now
  FileUtils.rm_f Dir.glob("#{DIR}/puma*").map { |f| f if now - File.mtime(f) > 20 }
end

def onesystemcall
  `find #{DIR} -name "puma*" -a -mmin +$((20/60)) -exec rm -f {} \;`
end

Benchmark.ips do |x|
  x.report('Delete each file') { eachfile }
  x.report('Delete files in one command') { onecommand }
  x.report('Delete files in one system call') { onesystemcall }

  x.compare!
end

以上是关于ruby 基准删除超过20秒的文件(目标:jRuby 1.7.4 Ubuntu 14.04)的主要内容,如果未能解决你的问题,请参考以下文章