From: Jim Freeze Date: 2005-07-16T03:49:14+09:00 Subject: Re: [QUIZ] Sampling (#39) > > $ time ./sample 5_000_000 1_000_000_000 > big_sample.txt > > > > real 30m10.593s > > user 29m54.544s > > sys 0m4.736s Here are my results on a lowly powerbook: time ./sample 5_000_000 1_000_000_000 > /dev/null 136.990u 1.120s 2:50.91 80.8% 0+0k 0+0io 0pf+0w time ./sample 5_000_000 1_000_000_000 > big_sample.txt 222.100u 2.500s 3:53.70 96.1% 0+0k 1+28io 0pf+0w % head big_sample.txt 402 421 549 571 757 1025 1224 1604 2064 2602 % tail big_sample.txt 999998571 999998613 999998644 999998648 999998841 999999116 999999414 999999609 999999609 999999915 Here's my typical interaction with a program. (I usually seem to be able to find all the ways that don't work first.) I've included a source listing of the application part of the code to demonstrate commandline. % ./sample Usage: sample [-h] number limit % ./sample -h NAME sample - Generate a list of samples SYNOPSIS sample [-h] number limit OPTIONS --help,-h Displays help page. % ./sample 10 9 ERROR: Number must be less than or equal to limit. % ./sample 10 11.1 ERROR: Argument 'limit' with value '11.1' must be an integer. Usage: sample [-h] number limit % ./sample 10.1 11 ERROR: Argument 'number' with value '10.1' must be an integer. Usage: sample [-h] number limit % ./sample 3 10 3 4 % ./sample 5 20 6 7 9 14 18 % cat sample #!/usr/bin/env ruby begin require 'commandline' rescue LoadError require 'rubygems' retry end class App < CommandLine::Application def initialize synopsis "[-h] number limit" short_description "Generate a list of samples" option :help expected_args :number, :limit end def convert_to_integer(*args) arg, val = nil, nil args.each { |arg| val = instance_variable_get("@#{arg}") instance_variable_set("@#{arg}", Integer(val)) } rescue raise "Argument '#{arg}' with value '#{val}' must be an integer.\n"+ usage end def main convert_to_integer :number, :limit puts Sample.create_sample_list(@number, @limit) end end#class App class Sample def self.create_sample_list(number, limit) Sample.new(number, limit).samples end #.... no peeking... #.... sorry, you won't find a spoiler here... -- Jim Freeze