SketchUcation Premium Membership

 

 

Optimization Tips

Re: Optimization Tips

Postby Jim » Wed Mar 17, 2010 2:39 pm

That's showing a for loop in the c language.
Jim
Global Moderator
 
Posts: 4126
Joined: Mon Nov 12, 2007 10:13 pm
Location: NEOH
Name: Jim
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: other
Level of SketchUp: Intermediate

Re: Optimization Tips

Postby thomthom » Wed Mar 17, 2010 2:45 pm

That's what it's doing under the hood.
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17545
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Optimization Tips

Postby Jim » Wed Mar 17, 2010 3:08 pm

thomthom wrote:That's what it's doing under the hood.


Right, so where is the definition for the for function?

The answer is there isn't one because for is not a function, but is "sugar". The for loop in Ruby really uses the .each method behind the scenes.

Although, I can't recall where I learned that. The link to the blog article mentions it, though.
Jim
Global Moderator
 
Posts: 4126
Joined: Mon Nov 12, 2007 10:13 pm
Location: NEOH
Name: Jim
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: other
Level of SketchUp: Intermediate

Re: Optimization Tips

Postby tbd » Wed Mar 17, 2010 3:24 pm

speaking of each vs for :

Code: Select all
loop1 = []
loop2 = []

calls = ["one", "two", "three"]

calls.each do |c|
  loop1 << Proc.new { puts c }
end

for c in calls
  loop2 << Proc.new { puts c }
end

loop1[1].call #=> "two"
loop2[1].call #=> "three"
SketchUp Ruby Consultant | Podium 1.x developer
http://plugins.ro
User avatar
tbd
 
Posts: 1018
Joined: Wed Nov 14, 2007 10:47 am
Location: Romania
Name: TBD

Re: FOR .. IN

Postby Dan Rathbun » Wed Mar 17, 2010 3:25 pm

Jim wrote: The for loop in Ruby really uses the .each method behind the scenes. ... Although, I can't recall where I learned that.

'Pick-Axe' > For ... In expressions
User avatar
Dan Rathbun
Top SketchUcator
 
Posts: 4069
Joined: Tue Oct 06, 2009 3:06 am
Location: Florida, USA
Name: Dan Rathbun
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: education
Level of SketchUp: Advanced

Re: Optimization Tips

Postby Jim » Wed Mar 17, 2010 3:28 pm

I guess to get back on topic, for loops are not faster then .each iterators. The performance must have to do with how the for loop variables are not loop scoped, as in each.
Jim
Global Moderator
 
Posts: 4126
Joined: Mon Nov 12, 2007 10:13 pm
Location: NEOH
Name: Jim
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: other
Level of SketchUp: Intermediate

Re: Optimization Tips

Postby thomthom » Sun Mar 21, 2010 8:32 pm

Came across this link:
http://www.h3rald.com/articles/efficien ... ut-review/

On that list it says
Use parallel assignment (a, b = 5, 6) where applicable

while at this link:
http://www.hxa.name/articles/content/ru ... _2007.html
Avoid parallel assignment

:roll:
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17545
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Optimization Tips

Postby thomthom » Sun Mar 21, 2010 10:55 pm

thomthom wrote:Came across this link:
http://www.h3rald.com/articles/efficien ... ut-review/

On that list it says
Use parallel assignment (a, b = 5, 6) where applicable

while at this link:
http://www.hxa.name/articles/content/ru ... _2007.html
Avoid parallel assignment

:roll:

I just bought the ebook and that review summary was wrong - parallel assignments are not recommended for performance important tasks.
Interesting read that book btw.
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17545
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Optimization Tips

Postby MartinRinehart » Thu Apr 01, 2010 3:06 pm

Let's see - for performance I'm going to avoid iterations, arrays, hashes and objects.

What's left?
Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.
MartinRinehart
 
Posts: 762
Joined: Mon Jul 27, 2009 1:13 pm
Name: Martin Rinehart

Re: Optimization Tips

Postby thomthom » Thu Apr 01, 2010 3:27 pm

MartinRinehart wrote:What's left?


puts "Hello World" :D
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17545
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Optimization Tips

Postby AdamB » Fri Apr 02, 2010 6:47 pm

Jim wrote:I guess to get back on topic, for loops are not faster then .each iterators. The performance must have to do with how the for loop variables are not loop scoped, as in each.


"Your racing car is not faster than my Trabant, it just covers more ground in a shorter time than my car." :-)
Developer of LightUp http://www.light-up.co.uk
User avatar
AdamB
LightUp
 
Posts: 744
Joined: Wed Dec 12, 2007 10:49 am
Location: Brighton, UK
Name: AdamB

Re: Optimization Tips

Postby cjthompson » Wed Apr 07, 2010 9:29 pm

Has anyone looked into Enumerable.grep()? it seems pretty useful, but I don't know how fast it is.
cjthompson
 
Posts: 152
Joined: Mon Jul 13, 2009 9:13 pm
Name: Chris Thomson

Re: Optimization Tips

Postby Jim » Wed Apr 07, 2010 9:57 pm

AdamB wrote:
Jim wrote:I guess to get back on topic, for loops are not faster then .each iterators. The performance must have to do with how the for loop variables are not loop scoped, as in each.


"Your racing car is not faster than my Trabant, it just covers more ground in a shorter time than my car." :-)


Heh? Oh. Yes, I see. :oops:

Would it be correct to say: An each loop can be as fast as a for loop if the loop variable has been initialized?
Jim
Global Moderator
 
Posts: 4126
Joined: Mon Nov 12, 2007 10:13 pm
Location: NEOH
Name: Jim
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: other
Level of SketchUp: Intermediate

Re: Optimization Tips

Postby thomthom » Wed Apr 07, 2010 10:02 pm

That would mean it's not the each loop itself that's slow - but the creation of variables.
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17545
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Optimization Tips

Postby Jim » Wed Apr 07, 2010 10:04 pm

Jim
Global Moderator
 
Posts: 4126
Joined: Mon Nov 12, 2007 10:13 pm
Location: NEOH
Name: Jim
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: other
Level of SketchUp: Intermediate

Re: Optimization Tips

Postby thomthom » Sun Apr 11, 2010 8:44 pm

Vertex.position is slow! Cache the result if you need to use the same Point3d multiple times.

Point3d.distance also accepts Vertex objects in place of Point3d or Array.
point1.distance(vertex2) is faster than point1.distance(vertex2.position).

http://www.thomthom.net/blog/2010/04/sk ... rformance/
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17545
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Optimization Tips

Postby AdamB » Tue Apr 13, 2010 9:32 am

Its all interesting info you're digging up thomthom, but I wonder where you're going..

Ruby is a scripting language that makes for very quick development, modern constructs and good readability. So you pay for that with execution performance. However, performance with a big P which may include how fast you can complete and deliver functionality may be better - but once again I do think you should play to Ruby's strength rather than perhaps bend it into something it isn't.

By the time you've created local copies of state, rewritten everything using a compact form etc etc you end up with something that is less readable and probably more prone to bugs. And as you've discovered, there is a massive difference in performance between native code and Ruby - such a large gulf, you're never going to come even close to closing it.

You should do heavy lifting with a C extension and GUI / API / semantic stuff with Ruby. Processing geometry topology with Ruby is, in general, not practical. Not that it can't be done..but that's not what I'm suggesting.
Developer of LightUp http://www.light-up.co.uk
User avatar
AdamB
LightUp
 
Posts: 744
Joined: Wed Dec 12, 2007 10:49 am
Location: Brighton, UK
Name: AdamB

Re: Optimization Tips

Postby thomthom » Tue Apr 13, 2010 9:54 am

AdamB wrote:Its all interesting info you're digging up thomthom, but I wonder where you're going..

That was actually stuff I found out before I got around to do a C extension.
Jumping from Ruby - or any other scripting language - C extensions is not an easy jump. If C isn't your cup of tea then it's worth knowing what saves time in Ruby. Most plugin writers here doesn't do C and have no interest in it either. Just making something that work - but still one can save noticeable time.

What I found most interesting in those test was that Vertex is a valid argument where the manual claims only Point3d. And passing the Vertex is faster than Vertex.position.


As for C extensions - it appear that there's a significant overhead of converting VALUEs to workable C types - so if you iterate only once over a set of data there isn't much to gain. Only if there's quite a bit more calculations.
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17545
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Optimization Tips

Postby AdamB » Tue Apr 13, 2010 11:54 am

thomthom wrote:As for C extensions - it appear that there's a significant overhead of converting VALUEs to workable C types - so if you iterate only once over a set of data there isn't much to gain. Only if there's quite a bit more calculations.

Not really. You asked the wrong question, so you perhaps got an answer that has misled you.

You asked about converting Ruby arrays to C etc. And everything I said stands. However, sounds like you actually want a C extension that operates upon the Ruby structures. If you have a situation where you are just wanting to twiddle existing Ruby data from C, it is well worth doing even for 1 pass because the fixed costs are pretty much zero.
Developer of LightUp http://www.light-up.co.uk
User avatar
AdamB
LightUp
 
Posts: 744
Joined: Wed Dec 12, 2007 10:49 am
Location: Brighton, UK
Name: AdamB

Re: Optimization Tips

Postby thomthom » Tue Apr 13, 2010 12:40 pm

I'm very green to this Ruby <-> C interaction - so its very likely I'm not doing thing the right way around.

AdamB wrote:However, sounds like you actually want a C extension that operates upon the Ruby structures.

What I have done so far is to calculate the soft selection for my Vertex Edit. So for each vertex in the selection set I needed to find the closest closest distance to any of the vertices not selected. It was the distance method that was so slow.
I did some tests - created a dummy set of 3d data in C and calculated the soft selection for that. Very fast. But as soon as I made the source data set come from RUBY it became very slow. The C function was setting two sets of ruby arrays of vertices. Getting the X,Y,Z data for each vertex seemed to be very slow - converting Vertex to Point3d and then converting the X,Y,Z into C doubles.
For every vertex in the selection I was iterating the remaining set of vertices and converting them.
What I then did was to do a pre-pass of the non-selected vertices and create an C array of point3d structs. I then got a big speed increase. That's what lead me to the impression that converting Ruby VALUES to C types are expensive.
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17545
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Optimization Tips

Postby AdamB » Tue Apr 13, 2010 3:46 pm

distance requires a square root of a scalar product. ie sqrt(A.B)

Keep in mind that in native "cpu" math, A.B is perhaps ~5 cycles and sqrt(X) is perhaps ~35 cycles. If you don't actually need the squareroot but just need to find the closest, then just compare A.B which should be significantly faster.
Developer of LightUp http://www.light-up.co.uk
User avatar
AdamB
LightUp
 
Posts: 744
Joined: Wed Dec 12, 2007 10:49 am
Location: Brighton, UK
Name: AdamB

Re: Optimization Tips

Postby thomthom » Tue Apr 13, 2010 4:16 pm

AdamB wrote:distance requires a square root of a scalar product. ie sqrt(A.B)

Keep in mind that in native "cpu" math, A.B is perhaps ~5 cycles and sqrt(X) is perhaps ~35 cycles. If you don't actually need the squareroot but just need to find the closest, then just compare A.B which should be significantly faster.

Yes - I was reading up on sqrt and found that to compare "longer" and "shorter" I didn't need sqrt. So I changed my code to only do the square root after I've found the shortest distance. That way it's called only once per vertex in Selection. (I needed the distance for some other calculations)
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17545
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Optimization Tips

Postby cjthompson » Tue Apr 13, 2010 4:50 pm

cjthompson wrote:Has anyone looked into Enumerable.grep()? it seems pretty useful, but I don't know how fast it is.


well, since no one seems to be listening... :cry:
I ran my own test (for: is using a for loop, grep: is using Enumerable.grep)
speedTest
for: entities - 0.016
grep: entities - 0.015
for: entities array - 0.0
grep: entities array - 0.016
for: range - 0.219
grep: range - 0.203
for: range array - 0.219
grep: range array - 0.218
for: strings - 0.469
grep: strings - 0.234
nil


here is the code I used:
Code: Select all
def speedTest
   entities = Sketchup.active_model.entities
   entitiesArray = entities.to_a
   range = 0..1000000
   rangeArray = range.to_a
   strings = range.collect{|number| number.to_s}
   
   ## Entities
   
   results = []
   start = Time.now
   for ent in entities
      if(ent.class == Sketchup::Edge)
         results << ent
      end
   end
   puts "for: entities - " + (Time.now - start).to_s
   
   results = []
   start = Time.now
   results = entities.grep(Sketchup::Edge)
   puts "grep: entities - " + (Time.now - start).to_s
   
   ## Entities array
   
   results = []
   start = Time.now
   for ent in entitiesArray
      if(ent.class == Sketchup::Edge)
         results << ent
      end
   end
   puts "for: entities array - " + (Time.now - start).to_s
   
   results = []
   start = Time.now
   results = entitiesArray.grep(Sketchup::Edge)
   puts "grep: entities array - " + (Time.now - start).to_s
   
   ## Range
   
   results = []
   start = Time.now
   for num in range
      if(num == 318256)
         results << num
      end
   end
   puts "for: range - " + (Time.now - start).to_s
   
   results = []
   start = Time.now
   results = range.grep(318256)
   puts "grep: range - " + (Time.now - start).to_s
   
   ## Range Array
   
   results = []
   start = Time.now
   for num in rangeArray
      if(num == 318256)
         results << num
      end
   end
   puts "for: range array - " + (Time.now - start).to_s
   
   results = []
   start = Time.now
   results = rangeArray.grep(318256)
   puts "grep: range array - " + (Time.now - start).to_s
   
   ## Strings
   
   results = []
   start = Time.now
   for str in strings
      if(str.match(/312\Z/))
         results << str
      end
   end
   puts "for: strings - " + (Time.now - start).to_s
   
   results = []
   start = Time.now
   results = range.grep(/312\Z/)
   puts "grep: strings - " + (Time.now - start).to_s
   
end


and the model I tested on:
Please, register (free) to access all the attachments on the forums.
cjthompson
 
Posts: 152
Joined: Mon Jul 13, 2009 9:13 pm
Name: Chris Thomson

Re: Optimization Tips

Postby thomthom » Tue Apr 13, 2010 4:54 pm

I also read that depending on the settings of the compiler the instruction set used to compute sqrt and it's performance vary greatly. One of the articles I read suggested that many compilers will use old set of instructions by default for greater compatibility.
What do you do for your projects?

Edit: one of the articles I read: http://assemblyrequired.crashworks.org/ ... uare-root/
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17545
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Optimization Tips

Postby Pout » Mon Apr 19, 2010 2:32 pm

concerning typename vs class:

For, till now, unexplained reason when i change typename with class the results are different
Script is a bit like this:

Code: Select all
x=entity.class (or entity.typename)
if x=="Face"
do something
elsif x=="Group"
do something
elsif x=="ComponentInstance"
do something
else
end


When the type is "ComponentInstance" the results are not the same for class and typename.
I need to check on this since the speed increase is huge
Pout
 
Posts: 272
Joined: Thu Aug 21, 2008 10:46 am

Re: Optimization Tips

Postby thomthom » Mon Apr 19, 2010 2:52 pm

.class returns a Class object - not a string.
What causes the slow down is the string comparison - that's what you want to avoid.

Code: Select all
x=entity.class
if x==Sketchup::Face
  do something
elsif x==Sketchup::Group
  do something
elsif x==Sketchup::ComponentInstance
  do something
else
end


or

Code: Select all
if entity.is_a?(Sketchup::Face)
  do something
elsif entity.is_a?(Sketchup::Group)
  do something
elsif entity.is_a?(Sketchup::ComponentInstance)
  do something
else
end


Update: fixed is_? to is_a?
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17545
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Optimization Tips

Postby Pout » Mon Apr 19, 2010 3:02 pm

I'll check and let you know. When i use 'class' the correct conditions are entered but the result differs.
I'll keep you posted if it changes with your scripts.

Thx
Pout
 
Posts: 272
Joined: Thu Aug 21, 2008 10:46 am

Re: Optimization Tips

Postby Pout » Fri May 07, 2010 9:41 am

all works, speeds increase is fine :)
thx!
Pout
 
Posts: 272
Joined: Thu Aug 21, 2008 10:46 am

Re: Optimization Tips

Postby thomthom » Fri May 07, 2010 9:50 am

:thumb:
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17545
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: thomthom
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Optimization Tips

Postby kwalkerman » Thu Jul 15, 2010 6:29 am

One thing I have noticed is that some code runs much slower with the outliner window open. Is there a way to close the window at the start of certain code execution, and then re-open it at the end?

--
Karen
kwalkerman
 
Posts: 135
Joined: Mon Feb 08, 2010 9:48 pm
Name: Karen

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago

Need SketchUp Books, Models, Styles or Textures? Check out our One Stop Shop for SketchUp.

Premium Members get 20% discount!

Ad Machine
Robot
 
Posts: 2012

PreviousNext

Return to Developers' Forum

Who is online

Users browsing this forum: No registered users and 6 guests