SketchUcation Premium Membership

 

 

Optimization Tips

Re: Optimization Tips

Postby thomthom » Tue Jan 25, 2011 6:37 pm

Gmanofdesign1 wrote:does the whole line have to be in c? im trying to map this out---
:sketchstatic:

What do you mean? Are you making a C Extension?
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17915
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: Thomas Thomassen
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Optimization Tips

Postby thomthom » Tue Jan 25, 2011 6:45 pm

Gmanofdesign1 wrote:THOM THOM WHAT KIND OF SCRIPTING LANGUAGE IS RUBY??????? :|

Sorry, but I don't understand what 'kind' you mean. Can you elaborate a bit more?

And please, do not use all caps. It's hard to read and it's considered bad manners.
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17915
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: Thomas Thomassen
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Optimization Tips

Postby Dan Rathbun » Wed Jan 26, 2011 12:45 am

Ruby is a 100% Object-Oriented Interpreted Scripting Language.

See: "Ruby Newbie's Guide to Getting Started"
User avatar
Dan Rathbun
Top SketchUcator
 
Posts: 4146
Joined: Tue Oct 06, 2009 3:06 am
Location: Florida, USA
Name: Dan Rathbun
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: education
Level of SketchUp: Advanced

Re: Optimization Tips

Postby Dan Rathbun » Wed Jan 26, 2011 1:14 am

Gmanofdesign1 wrote: does the whole line have to be in c? im trying to map this out---

If you are new to Ruby... learn Ruby scripting, don't worry about it's C source code, you'll just confuse yourself. (The Ruby interpreter engine just happens to be written in C and compiled. You don't need to know C unless your involved with actually maintaining / updating the Ruby Core libraries. This has noting to do with using Ruby or writing Ruby scripts, or using Sketchup.)
User avatar
Dan Rathbun
Top SketchUcator
 
Posts: 4146
Joined: Tue Oct 06, 2009 3:06 am
Location: Florida, USA
Name: Dan Rathbun
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: education
Level of SketchUp: Advanced

Re: Optimization Tips

Postby thomthom » Tue Feb 01, 2011 7:41 pm

i += 1 vs i = i.next

i=0; t=Time.now; 10000000.times { i+=1 }; Time.now-t
2.045

i=0; t=Time.now; 10000000.times { i=i.next }; Time.now-t
1.682
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17915
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: Thomas Thomassen
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Optimization Tips

Postby TIG » Tue Feb 01, 2011 8:46 pm

thomthom wrote:i += 1 vs i = i.next
i=0; t=Time.now; 10000000.times { i+=1 }; Time.now-t
2.045
i=0; t=Time.now; 10000000.times { i=i.next }; Time.now-t
1.682

So avoid i='0'; t=Time.now; 10000000.times { i.next! }; Time.now-t
~8.300 :roll:
TIG
User avatar
TIG
Global Moderator
 
Posts: 14310
Joined: Mon Nov 12, 2007 7:24 pm
Location: Northumbria UK
Name: TIG
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Optimization Tips

Postby thomthom » Tue Feb 01, 2011 9:54 pm

thomthom wrote:That would mean it's not the each loop itself that's slow - but the creation of variables.


range = (0..10000000)

t=Time.now; range.each { |i| x = i + 1 }; Time.now-t
3.402

t=Time.now; x=0; range.each { |i| x = i + 1 }; Time.now-t
2.848

t=Time.now; x=0; i=0; range.each { |i| x = i + 1 }; Time.now-t
2.39

t=Time.now; for j in range; y = j + 1; end; Time.now-t
2.196

t=Time.now; y=0; for j in range; y = j + 1; end; Time.now-t
2.186

If one has to use blocks, init the variables you use inside the block first.
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17915
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: Thomas Thomassen
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Optimization Tips

Postby dany67300 » Fri Feb 18, 2011 6:19 pm

I have read all you optimisation tips and tried them, but nothing seems to change the speed creation of my objects. I'm using Sketchup 8 to create dominos described by a picture. To create the dominos, I tried the add_face method and the fill_from_mesh, but the times are exactly the same. It takes me about 2 s to create 400 pieces, and it's growing exponentially. With 600 pieces -> 7s, 1200 pcs -> 50s...
Is it normal to take so much time ? Each domino is created in his own group for the moment, but it doesn't change if I create them directly in my scene.
dany67300
 
Posts: 9
Joined: Wed Jul 21, 2010 3:31 pm

Re: Optimization Tips

Postby TIG » Fri Feb 18, 2011 6:26 pm

dany67300 wrote:I have read all you optimization tips and tried them, but nothing seems to change the speed creation of my objects. I'm using Sketchup 8 to create dominoes described by a picture. To create the dominoes, I tried the add_face method and the fill_from_mesh, but the times are exactly the same. It takes me about 2 s to create 400 pieces, and it's growing exponentially. With 600 pieces -> 7s, 1200 pces -> 50s...
Is it normal to take so much time ? Each domino is created in his own group for the moment, but it doesn't change if I create them directly in my scene.

Since all dominoes are fixed by there number pattern, why not make the set as separate SKPs with common origins.
Then load them into the model when you run the script - no need to make geometry at all - and 'entities.add_instance(defn, trans)' of them as needed - the transformation used when adding determines the location and rotation.
Because they are each component instances you can swap one type for another as you wish - in code instance.definition=xxxx ...
IF you only have one simple block domino make one definition and add_instances of that multiple times... You can apply different materials separately to each instance... :geek:
TIG
User avatar
TIG
Global Moderator
 
Posts: 14310
Joined: Mon Nov 12, 2007 7:24 pm
Location: Northumbria UK
Name: TIG
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Optimization Tips

Postby dany67300 » Mon Feb 21, 2011 11:36 am

I hadn't seen that i could put a different material to each instance of a same defintion :oops:
thanks a lot ! it works very well :)
dany67300
 
Posts: 9
Joined: Wed Jul 21, 2010 3:31 pm

Re: Optimization Tips

Postby bentleykfrog » Wed Mar 02, 2011 11:02 am

dany67300 wrote:It takes me about 2 s to create 400 pieces, and it's growing exponentially. With 600 pieces -> 7s, 1200 pcs -> 50s...
Is it normal to take so much time ? Each domino is created in his own group for the moment, but it doesn't change if I create them directly in my scene.


I've noticed that sketchup slows down greatly once the number of groups in the current tier is greater than 1000 on my machine. Does your script speed up if the geometry is written straight to Sketchup.active_model.entities?
bentleykfrog
 
Posts: 84
Joined: Thu Sep 10, 2009 2:27 am
Location: Adelaide, Australia
Name: Niall Campbell

Re: Optimization Tips

Postby thomthom » Wed Mar 02, 2011 11:12 am

bentleykfrog wrote:
dany67300 wrote:It takes me about 2 s to create 400 pieces, and it's growing exponentially. With 600 pieces -> 7s, 1200 pcs -> 50s...
Is it normal to take so much time ? Each domino is created in his own group for the moment, but it doesn't change if I create them directly in my scene.


I've noticed that sketchup slows down greatly once the number of groups in the current tier is greater than 1000 on my machine. Does your script speed up if the geometry is written straight to Sketchup.active_model.entities?

Adding entities to SketchUp slows down in direct proportion to how many existing entities there is in the entities collection you add to.
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17915
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: Thomas Thomassen
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Optimization Tips

Postby sm4rt » Sat Apr 09, 2011 12:04 am

Well I got a situation !! :shock:

C:\>ruby test.rb
range = (0..90000000)
t=Time.now; x=0; i=0; range.each { |i| x = 0b0011_1100<<2 }; Time.now-t
13.156753
t=Time.now; x=0; i=0; range.each { |i| x = 60*4 }; Time.now-t
10.400594


just a no sens !!!
Really a human oriented language ;)
sm4rt
 
Posts: 17
Joined: Fri Apr 09, 2010 11:02 am

Re: Optimization Tips

Postby Dan Rathbun » Sat Apr 09, 2011 3:19 am

The for loop should be faster, try:

t = Time.now
for i in range do
# code here
end
puts Time.now - t
User avatar
Dan Rathbun
Top SketchUcator
 
Posts: 4146
Joined: Tue Oct 06, 2009 3:06 am
Location: Florida, USA
Name: Dan Rathbun
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: education
Level of SketchUp: Advanced

Re: Optimization Tips

Postby sm4rt » Sat Apr 09, 2011 4:13 am

Was talking about shifting binary number is longer then the same "base 10" arithmetic operation...

Which is no sense in processor calculation.
Try the same comparison in ASM, C++, PHP etc. and look the result^^

But in this case I think it's because x = 0b0011_1100<<2 affect the decimal number of the binary one to x variable so the number of edge clock needed is greater... IMO


Edit: And for loop isn't for me
Result-for-each-variables.txt

here is my results of the test that ThomThom put above to prove that for loop is better then each one and that declaring variable before is faster too but it's still not true for my equipment...
(Ruby 1.9.2-p180 / Windows 7 64 bit / Intel Core i3 M 350 2.27GHz)

So I think that these optimizations depend of many variables....(versions of Ruby/Sketchup) Even if some will still be true in the future...
Please, register (free) to access all the attachments on the forums.
sm4rt
 
Posts: 17
Joined: Fri Apr 09, 2010 11:02 am

Re: Optimization Tips

Postby glro » Sun Aug 19, 2012 2:09 pm

Dan Rathbun wrote:
Dan Rathbun wrote:its nice but...
The code needs updating. It needs to search by ID instead.
(Or have arrays of the Inspector captions in all the local versions.)

Ooops.. just checked. The Outliner does not have an ID.
But Jim's system call 'may' work. The window object can have a different "name" than the text displayed on the caption bar.
Someone running a non-English version could test it and let us know.


I run a spanish computer using french as default language, and it doesn't work...

But there is a simple way to do it, using the standard line of code you mentioned, plus a messagebox

Code: Select all
result = UI.messagebox "if the outliner window is opened, close it?'", MB_YESNO
  if result == 6 #yes
     #close or open the outliner window
      status=UI.show_inspector "Outliner"
      if status==false then
        UI.show_inspector "Outliner"
      end
 end


This way, you don't toggle on the outliner window if it is not opened already, and if it is, you close it
glro
 
Posts: 135
Joined: Mon Nov 30, 2009 9:45 am
Location: Spain
Name: Georges LE ROUX
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: engineering and mechanical design
Level of SketchUp: Intermediate

Re: Optimization Tips

Postby Dan Rathbun » Sun Aug 19, 2012 3:30 pm

Actually we cannot close inspectors singly. Once they are open, we can only collapse or expand them.
User avatar
Dan Rathbun
Top SketchUcator
 
Posts: 4146
Joined: Tue Oct 06, 2009 3:06 am
Location: Florida, USA
Name: Dan Rathbun
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: education
Level of SketchUp: Advanced

Re: Optimization Tips

Postby TIG » Sun Aug 19, 2012 3:44 pm

For Windows windows only - using Win32API.so - which you'll need to 'require'...
You can 'close' just one window thus:
closeWindow("Outliner")
where:
Code: Select all
def closeWindow(name)
    findWindow = Win32API.new("user32.dll","FindWindow",['P','P'],'N')
    pw=findWindow.call(0,name)
    sendMessage = Win32API.new("user32.dll","SendMessage",['N','N','N','P'],'N')
    sendMessage.call(pw,0x0112,0xF060,0)#CLOSES
end
You can check if a window is 'visible' with:
Code: Select all
def windowIsVisible?(name)
    findWindow = Win32API.new("user32.dll","FindWindow",['P','P'],'N')
    isWindowVisible= Win32API.new("user32.dll","IsWindowVisible",['P'],'N')
    pw=findWindow.call(0,name)
    return isWindowVisible.call(pw)==1
end
Incidentally, the roll 'up'/'down' methods I often use are:
Code: Select all
def toggleRollUp(name)
    findWindow = Win32API.new("user32.dll","FindWindow",['P','P'],'N')
    pw=findWindow.call(0,name)
    sendMessage = Win32API.new("user32.dll","SendMessage",['N','N','N','P'],'N')
    sendMessage.call(pw,0x00a1,2,"")#WM_NCLBUTTONDOWN
    sendMessage.call(pw,0x0202,0,"")#WM_LBUTTONUP
end
def isRolledUp?(name)
    findWindow = Win32API.new("user32.dll","FindWindow",['P','P'],'N')
    getWindowRect= Win32API.new("user32.dll","GetWindowRect",['P','PP'],'N')
    pw=findWindow.call(0,name)
    data=Array.new.fill(0.chr,0..4*4).join
    getWindowRect.call(pw,data)
    rect=data.unpack("i*")
    #if window height is less than 90 then the window is rolledup
    return (rect[3]-rect[1]) < 90
end
... using isRolledUp?("Outliner") to then toggleRollUp("Outliner") to roll it up if it's down etc...
TIG
User avatar
TIG
Global Moderator
 
Posts: 14310
Joined: Mon Nov 12, 2007 7:24 pm
Location: Northumbria UK
Name: TIG
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Optimization Tips

Postby glro » Mon Aug 20, 2012 8:50 am

Dan Rathbun wrote:Actually we cannot close inspectors singly. Once they are open, we can only collapse or expand them.


i am surely missing something

you are right; the window is not closed, only collapsed

but it is sufficient; my experience is that sketchup doesn't crash anymore
glro
 
Posts: 135
Joined: Mon Nov 30, 2009 9:45 am
Location: Spain
Name: Georges LE ROUX
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: engineering and mechanical design
Level of SketchUp: Intermediate

Re: Optimization Tips

Postby TIG » Mon Aug 20, 2012 9:20 am

Collapsing [rolling-up] the Outliner is sufficient to stop it updating and causing bugsplats.
However, my methods just posted do also 'close' the window if desired - but this might be annoying for users [?]... remember to use the 'locale' name for the window...
TIG
User avatar
TIG
Global Moderator
 
Posts: 14310
Joined: Mon Nov 12, 2007 7:24 pm
Location: Northumbria UK
Name: TIG
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Optimization Tips

Postby thomthom » Fri Oct 12, 2012 12:11 pm

Page 152
http://www.slideshare.net/tenderlove/zo ... de-so-slow

attr_accessor :property vs def property; @property; end

attr_accessor wins.

Video of the presentation where the linked slideshow was used: http://confreaks.com/videos/427-rubycon ... de-so-slow
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17915
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: Thomas Thomassen
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Optimization Tips

Postby Dan Rathbun » Fri Oct 12, 2012 7:18 pm

That would be in the sub-catagory of load optimization.

However, later is there any difference when instances are instantiated ??

:?:
User avatar
Dan Rathbun
Top SketchUcator
 
Posts: 4146
Joined: Tue Oct 06, 2009 3:06 am
Location: Florida, USA
Name: Dan Rathbun
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: education
Level of SketchUp: Advanced

Re: Optimization Tips

Postby thomthom » Fri Oct 12, 2012 10:05 pm

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

Re: Optimization Tips

Postby Dan Rathbun » Sat Oct 13, 2012 2:12 am

The attr_* creation call is run on the C side so is bound to be faster. There is no parsing of text characters that make up the method definition, and translating to C-calls.

Also the built-in creates the @var and sets it to nil, so the pure Ruby version would also need to do that (within the initialize method, just to be fair.)


This work is all defintion work, done when the class is parsed and defined. It is only done once.

Who's classes have a million accessor methods that need to be defined ?

What I mean?
.. is that later, at Runtime, when actually calling the accessor method, to get the value of the instance variable, is there a speed difference between the method created by the C-call, and the method created by the Pure Ruby definition ?

I read the example as measuring the difference in method instance creation times. (Even methods are instances of a class object.)
User avatar
Dan Rathbun
Top SketchUcator
 
Posts: 4146
Joined: Tue Oct 06, 2009 3:06 am
Location: Florida, USA
Name: Dan Rathbun
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: education
Level of SketchUp: Advanced

Re: Optimization Tips

Postby thomthom » Sat Oct 13, 2012 9:48 am

Have a look at the slideshow linked - from page 152 - it displays what does on on the C side and explains the difference. It also shows graphs for the speed difference.

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

Re: Optimization Tips

Postby Dan Rathbun » Sat Oct 13, 2012 4:07 pm

I did.. It is not clear.
User avatar
Dan Rathbun
Top SketchUcator
 
Posts: 4146
Joined: Tue Oct 06, 2009 3:06 am
Location: Florida, USA
Name: Dan Rathbun
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: education
Level of SketchUp: Advanced

Re: Optimization Tips

Postby thomthom » Tue Oct 23, 2012 11:30 am

Dan Rathbun wrote:I did.. It is not clear.

Page 154 vs 155 - you can see it does quite a lot of different things. On 154 which is the code for attr_reader it just directly fetches the value. In page 155 you can see it invokes a whole lot more (explained partly on page 156).
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17915
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: Thomas Thomassen
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Optimization Tips

Postby jolran » Tue Mar 12, 2013 1:24 pm

if vector1.samedirection?(vector2) => do something.... end

seams a little faster than:

next unless vector1.samedirection?(vector2) => do something...



Havent done any vigourious testing, could be specific case for me or maybe just a difference between if and unless.

Just wanted to mention I noticed some difference in speed for the 2 cases.
User avatar
jolran
 
Posts: 678
Joined: Sun Oct 26, 2008 9:23 pm
Location: Sweden
Name: Joel G
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: other
Level of SketchUp: Intermediate

Re: Optimization Tips

Postby thomthom » Tue Mar 12, 2013 2:32 pm

Got some numbers? I'd be surprised if there was a change due to if vs unless.

Isn't it the "do something" that makes the difference here? Because you're comparing inverted logic that control whether "do something" is executed or not...
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17915
Joined: Tue Nov 13, 2007 12:47 pm
Location: Trondheim, Norway
Name: Thomas Thomassen
Operating system: Windows
SketchUp version: 2013
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: Optimization Tips

Postby jolran » Tue Mar 12, 2013 4:30 pm

We are talking ms here, but still a consistent difference for me.

I was doing the condition inside a for loop.

Isn't it the "do something" that makes the difference here? Because you're comparing inverted logic that control whether "do something" is executed or not...


Maybe, I don't quite know the difference :oops:

This was at the end of the loop, so the loop would restart again anyway if the "if" statement was false and there where more items to process.
My theory was to shortcut whats inside the if statement and just go ahead to the next one. But that was slower..

I have also noticed the same kind of ms speedgain when using if @edge VS if @edge == true while setting
true or false elsewhere in the script.
But that is probably more logic, although one could possibly expect that only if @edge needs to do more lookups.

Again I could be overlooking something fundamental gotcha in Ruby, needs testing by others.
Maybe next time I'll test I 'll get opposite results :)

Edit: It could have something to do with that the if statement has an end in this case?
So the code get's encapsulated or something.
next unless vector1.samedirection?(vector2) => do something... end. Doesent work.

Anyway it shaved 2 seconds of a process that took 30 seconds.
User avatar
jolran
 
Posts: 678
Joined: Sun Oct 26, 2008 9:23 pm
Location: Sweden
Name: Joel G
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: other
Level of SketchUp: Intermediate

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 3 guests