Speeding Up Extension Loading

I KNOW that comparing long absolute pathnames against a every member in a large array of long absolute pathnames is going to be a time waster as compared to a simple quick boolean test (the value held in a reference local to the plugin code where it will never clash with any other external reference.)

But for a quick test …

def test(loop = 500)
  loop = loop.to_i

  checkpath = $".last
  t1 = Time.now
  loop.times do |i|
    if $".any? {|path|
      path == checkpath
    }
      true
    end
  end
  t1end = Time.now

  @loaded = false
  t2 = Time.now
  loop.times do |i|
    if !@loaded
      false
    end
  end
  t2end = Time.now
  
  t1time = t1end.to_f - t1.to_f
  t2time = t2end.to_f - t2.to_f

  puts "Results: #{loop} times"
  puts
  puts "Test absolute string pathname comparison: #{t1time} seconds"
  puts
  puts "Test boolean reference comparison: #{t2time} seconds"
  puts
  puts "Conclusion: absolute string pathname comparison is #{t1time/t2time} times longer."

end

500 times …

test
Results: 500 times

Test absolute string pathname comparison: 0.008013010025024414 seconds

Test boolean reference comparison: 0.0 seconds

Conclusion: absolute string pathname comparison is Infinity times longer.

50,000 times …

Results: 50000 times

Test absolute string pathname comparison: 0.7010889053344727 seconds

Test boolean reference comparison: 0.001995086669921875 seconds

Conclusion: absolute string pathname comparison is 351.40774378585087 times longer.

Now …

I may be a bit bombastic in saying “SketchUp startup times suffer” but it’s not the main reason why I do not use the “sketchup.rb” file_loaded? method.

It’s principle that the test is a boolean one, and pushing long pathname strings into a global array and then walking that array to compare loaded files is not the best practice nor the fastest.

It’s better and faster to rely upon the internal extension itself to know it’s own loaded state.

There are no features in “sketchup.rb” that are not better done by the extension author and their code itself, and then the extension does not need to even require “sketchup.rb”.

2 Likes