LoginSignup
0
0

More than 5 years have passed since last update.

横浜rb #75

Last updated at Posted at 2016-12-10

レシピブック 240

mac で ruby で fiddle で cat

#MacOS で fiddle で cat
require "fiddle/import"

module Libc
  extend Fiddle::Importer
  dlload "/usr/lib/libSystem.B.dylib"
  extern "FILE * fopen( char *, char* )"
  extern "int fgetc(FILE*)"
  extern "int fclose(FILE*)"
  EOF=-1
end

def cat(path)
  fp=Libc.fopen( path, "r" )
  while( ( c=Libc.fgetc(fp)  ) !=  Libc::EOF ) do
    print c.chr
  end
  Libc.fclose(fp)
end

ARGV.each do |path|
  cat path
end

レシピブック 241

sleep の返戻値が無意味な感じ。
毎回違うけど、手元で動かしたらこんな感じ:

10.times.map{ sleep 0.4 } #=> [1, 0, 0, 1, 0, 1, 0, 0, 1, 0]

レシピブック 242

ベンチマークは Benchmark.bmbm がお勧め。

require 'benchmark'

M=10000
N=1000000

Benchmark.bmbm do |x|
  x.report("power") { M.times{ 2**N+2**(N+1)} }
  x.report("shift") { M.times{(1<<N)+(1<<(N+1))} }
end
Rehearsal -----------------------------------------
power   2.420000   1.020000   3.440000 (  3.450832)
shift   0.590000   0.340000   0.930000 (  0.921296)
-------------------------------- total: 4.370000sec

            user     system      total        real
power   2.350000   0.880000   3.230000 (  3.238570)
shift   0.540000   0.240000   0.780000 (  0.772868)

と、リハーサルが走ってから本番を測る。

意外と速度に差がある。

レシピブック 243

邪悪な open の使い方

open("|df -k", "r") do |s|
  p s.gets
end

open の引数の冒頭に | を書くと、コマンドの実行になる。こわい。

これを避けるために、 File.open と書くと安全。

それと。
Open3 は windows でも使える。いつからなのかは不明。

ペパボの研修

素晴らしい。羨ましい。

AWS

自分用メモ:Amazon Rekognition を調査する。

Load Average

辺りが参考になる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0