Skip to content

Formatting of pasted code is broken #47

@schneems

Description

@schneems

When I paste in this code into IRB:

require 'thread'
def fib( n )
  return  n  if ( 0..1 ).include? n
  ( fib( n - 1 ) + fib( n - 2 ) )
end
thread_execution_time = Hash.new {|h, k| h[k] = [] }
mutex = Mutex.new
cv = ConditionVariable.new
execution_time = []
thread_count = 1
work_count = 512
(1..128).each do |thread_count|
  cpu_work = Array.new(work_count, 20)
  threads = []
  thread_count.times.each do
    threads << Thread.new do
      mutex.synchronize { cv.wait(mutex) }
      while fib_work = cpu_work.pop
        fib(fib_work)
      end
    end
  end
  sleep 0.1
  puts "Working #{work_count} with #{thread_count} threads"
  last = Time.now
  cv.broadcast()
  threads.map(&:join)
  execution_time[thread_count] = Time.now - last
  execution_time
end
execution_time.each_with_index do |x, i|
  puts "#{i}, #{x}"
end

I get this result:

irb(main):006:0> thread_execution_time = Hash.new {|h, k| h[k] = [] }
irb(main):007:0> mutex = Mutex.new
irb(main):008:0> cv = ConditionVariable.new
irb(main):009:0> execution_time = []
irb(main):010:0> thread_count = 1
irb(main):011:0> work_count = 512
irb(main):012:0* (1..128).each do |thread_count|
irb(main):013:0* cpu_work = Array.new(work_count, 20)
irb(main):014:0* threads = []
irb(main):015:0* thread_count.times.each do
irb(main):016:0* threads << Thread.new do
irb(main):017:0* mutex.synchronize { cv.wait(mutex) }
irb(main):018:1* while fib_work = cpu_work.pop
irb(main):019:1*   fib(fib_work)
irb(main):020:0* end
irb(main):021:-* end
irb(main):022:-* end
irb(main):023:-* sleep 0.1
irb(main):024:-* puts "Working #{work_count} with #{thread_count} threads"
irb(main):025:-* last = Time.now
irb(main):026:-* cv.broadcast()
irb(main):027:-* threads.map(&:join)
irb(main):028:-* execution_time[thread_count] = Time.now - last
irb(main):029:-* execution_time
irb(main):030:-> end

Note that all the whitespace is gone in the IRB console

$ gem list | grep irb
irb (1.2.0, default: 1.0.0)
⛄ 2.6.5 🚀  ~/Documents/projects/ruby (trunk)
$ ruby -v
ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin17]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions