Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenStudio::Vector when SWIG'ed into ruby is not enumerable #3076

Closed
rHorsey opened this issue Mar 26, 2018 · 1 comment · Fixed by #3989
Closed

OpenStudio::Vector when SWIG'ed into ruby is not enumerable #3076

rHorsey opened this issue Mar 26, 2018 · 1 comment · Fixed by #3989

Comments

@rHorsey
Copy link
Contributor

rHorsey commented Mar 26, 2018

As a result, iteration through the vector must be done with for loops, and ruby-isms cannot be used. If enumerable was included, this would allow each, all?, map, select, <<, ect. to be used, which is the expected and default behavior for iteration over array-type data-structures in ruby. The current workaround is to cast the OS Vector to a string, and then eval it. This is obviously very sub-optimal.

@rHorsey
Copy link
Contributor Author

rHorsey commented Mar 26, 2018

One good piece of news - I tested the performance of casting vs looping, and (I assume) thanks to the staticly linked binaries, the speed of looping is really good now. I'll replace the random OS Vector with one from a sql file and confirm, but it looks like this is just a interface issue.

require 'openstudio'
require 'benchmark'
# Make an array of random 1min ts data
os_vec = OpenStudio::Vector.new(525600)
values = Array.new(525600) { rand() * 5000 }
for i in 1..525599
  os_vec[i] = values[i]
end
Benchmark.bm(1) do |x|
  x.report("cast:") {
    # cast the vector to a string and then cast it to an array of strings
    quick_proc = os_vec.to_s.split(',')
    # the first and last have some cleanup items because of the Vector method
    quick_proc[0] = quick_proc[0].gsub(/^.*\(/, '')
    quick_proc[-1] = quick_proc[-1].gsub(")", '')
    # convert each of the values to a float
    quick_proc.map! { |val| val.to_f }
  }
  x.report("loop:") {
    # loop through each value to retrieve it
    slow_proc = []
    for i in 1..os_vec.length - 1
      slow_proc << os_vec[i]
    end
  }
end

with the result

        user     system      total        real
cast:  0.350000   0.020000   0.370000 (  0.383714)
loop:  0.070000   0.000000   0.070000 (  0.074864)

jmarrec added a commit that referenced this issue Jun 2, 2020
@jmarrec jmarrec self-assigned this Jun 2, 2020
tijcolem added a commit that referenced this issue Aug 7, 2020
Fix #3076 - make openstudio::Vector "Enumerable" in ruby
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants