SketchUcation Plugin Store

 

 

Optimization Tips

Re: Optimization Tips

Postby AdamB » Mon Feb 01, 2010 11:23 am

OT: Any chance the forum administrator of SCF can fix the [ruby] tag to not remove formatting. Formatting is a big part of understanding code, and while for regular text collapsing whitespace down to a single space might work, for code it does not.
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 » Mon Feb 01, 2010 11:29 am

AdamB wrote:OT: Any chance the forum administrator of SCF can fix the tag to not remove formatting. Formatting is a big part of understanding code, and while for regular text collapsing whitespace down to a single space might work, for code it does not.

I think the [ruby]ruby is meant for inline code. While you got the code tag for block codes. (Though I wish there was a way to expand it - I loathe internal scrollbars.)
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17544
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 » Mon Feb 01, 2010 11:33 am

Interesting test Adam:

doit
6.474
3.292
nil


Note: I increased the number of iterations (10000000.times { ... })
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17544
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 » Mon Feb 01, 2010 11:35 am

Didn't realise Ruby would recreate the variables for each iteration. I'd thought it'd keep them for the duration of the loop...
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17544
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 » Mon Feb 01, 2010 11:36 am

Seems an arbitrary (and wrong) assumption that inline code requires removing whitespace. Why not just leave in what the author wrote rather than trying to second guess? Whatever.
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 » Mon Feb 01, 2010 11:38 am

Agree - whitespace eating of ruby has bothered me as well. Will ask if it can be changed.
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17544
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 » Mon Feb 01, 2010 11:41 am

thomthom wrote:Didn't realise Ruby would recreate the variables for each iteration. I'd thought it'd keep them for the duration of the loop...


The closure you create with curly braces is handled as a first class object and passed as an argument to the iterator. This means the scope of any variables you mention inside that block is limited to that block - it must create them each time. :-(
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 » Mon Feb 01, 2010 12:19 pm

Is that why each is slow?
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17544
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 Gaieus » Tue Feb 02, 2010 9:10 am

Guys, Thom asked if we can do something with these white spaces but I have to say it is most probable that we cannot. I is hard coded somewhere in the php script of the forum software and even if we could tweak that, it would be impossible to keep it through upgrades (which is very due soon anyway).

Is the code tag not good (apart from that scrolling annoyance)?
Gai...
User avatar
Gaieus
Administrator
 
Posts: 27601
Joined: Sat Oct 20, 2007 8:24 am
Location: Pécs, Hungary
Name: Csaba Pozsárkó
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: historical reconstruction
Level of SketchUp: Advanced

Re: Optimization Tips

Postby thomthom » Tue Feb 02, 2010 9:37 am

It's ok. I just hoped there was a config UI for BBCode tags on the forum. Thought it was normal. The code tag is ok, just figured if it could be changed...

I don't suppose there are forum plugins that can be installed? having the code block apply syntax highlighting would be a delight for us coders. Such as this:

http://code.google.com/p/syntaxhighlighter/



Edit: what version of phpBB does SCF run? I'm looking at this: http://www.phpbb.com/kb/article/adding- ... in-phpbb3/ from this it appear to be that it'd be a matter of setting the HTML replacement for the ruby tag to not collapse white space using CSS.

Replacement sample something like this:
<span style="white-space:pre;">{TEXT}</span>
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17544
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 Gaieus » Tue Feb 02, 2010 11:02 am

I can imagine you would like that syntax highlight! I use Notepad++ and know what a difference it is!

Coen and Tavi should be spoken to about these things.
Gai...
User avatar
Gaieus
Administrator
 
Posts: 27601
Joined: Sat Oct 20, 2007 8:24 am
Location: Pécs, Hungary
Name: Csaba Pozsárkó
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: historical reconstruction
Level of SketchUp: Advanced

Re: Optimization Tips

Postby thomthom » Mon Mar 08, 2010 1:25 pm

AdamB wrote:I see a lot of SU scripts using some of the more compact iterators Ruby iterators. So they might read nice, but they're often slower than just simple for-loops.

http://blog.shingara.fr/each-vs-for.html

In regard to this should one init the variables used by for in to speed up things? or is that not needed?

Would this
x = 0
for x in collection
# ...
end

be faster than
for x in collection
# ...
end
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17544
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 » Wed Mar 17, 2010 12:35 pm

no
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 Jim » Wed Mar 17, 2010 2:24 pm

I've always thought for used each under the hood.

http://blog.grayproductions.net/article ... e_for_loop

for loops do not have their own scope - the loop variable and any variables created in the loop become available (or are over-written) in the current scope.

With .each, variables are local to the block {..}
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:33 pm

Jim wrote:I've always thought for used each under the hood.

http://blog.grayproductions.net/article ... e_for_loop

for loops do not have their own scope - the loop variable and any variables created in the loop become available (or are over-written) in the current scope.


If you click the method names in the Ruby API manual you get to see the sourcecode:
http://ruby-doc.org/core/classes/Array.html#M002173
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom
Global Moderator
 
Posts: 17544
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 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: 17544
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: 17544
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: 17544
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: 17544
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: 17544
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

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago

Vertex Tools for SketchUp. Take control over each vertex with this vertex editor for SketchUp.

Premium Members get 20% discount!

Ad Machine
Robot
 
Posts: 2012

Next


 

Return to Developers' Forum

Who is online

Users browsing this forum: Bing [Bot], Dik Harrison, Polygony and 2 guests