sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Optimization Tips

    Scheduled Pinned Locked Moved Developers' Forum
    110 Posts 22 Posters 168.8k Views 22 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • thomthomT Offline
      thomthom
      last edited by

      @unknownuser said:

      I have just found out that converting String to Length directly is up to 13x slower in comparision to converting it to Float first and only then to Length...

      That is useful to know. But that assumes one has a string with only a numeral.
      String.to_l will allow you to covert strings such as '20m' and '20mm'. With out any length unit indication in the string it will assume the length is in the unit of the current model.

      Thomas Thomassen β€” SketchUp Monkey & Coding addict
      List of my plugins and link to the CookieWare fund

      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        BUT remember that .to_l parses any 'units' text to work out the actual value into inches...
        So "1.0m".to_l >>> 39.3700787401575"
        or "1'".to_l >>> 12"
        BUT
        "1.0m".to_f.to_l >>> 1.0"
        and "1'".to_f.to_l >>> 1"
        therefore you may as well miss out the second method .to_l as
        "1.0m".to_f >>> 1.0
        and "1'".to_f >>> 1
        i.e. as a 'raw number'... AND 'raw numbers' are assumed to be in inches anyway == 1.0"...
        Also .to_l and .to_f work differently if there is no 'unit' suffix...
        If you have mm set as your current units then
        "1".to_l >>> 0.0393700787401575 (inches)
        but "1".to_f >>> [ruby:3h6c8mbs]1.0[/ruby:3h6c8mbs] (float/number),
        and with inches as the current units
        "1".to_l >>> [ruby:3h6c8mbs]1[/ruby:3h6c8mbs] (inch)
        SO if you have an input that might be in anything other than inches and might have units in its string you do need to use .to_l or you risk returning a wrong value... πŸ€“

        TIG

        1 Reply Last reply Reply Quote 0
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by

          + vs << vs "#{}"

          Benchmark Test (at ruby-talk-google)
          String concatenation in ruby

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • thomthomT Offline
            thomthom
            last edited by

            ` t=Time.now; 1000000.times{ 3**2 }; puts Time.now - t
            0.948
            nil

            t=Time.now; 1000000.times{ 3*3 }; puts Time.now - t
            0.216
            nil`

            Thomas Thomassen β€” SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund

            1 Reply Last reply Reply Quote 0
            • thomthomT Offline
              thomthom
              last edited by

              @unknownuser said:

              does the whole line have to be in c? im trying to map this out---
              %(#0040FF)[]

              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

              1 Reply Last reply Reply Quote 0
              • thomthomT Offline
                thomthom
                last edited by

                @unknownuser said:

                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

                1 Reply Last reply Reply Quote 0
                • Dan RathbunD Offline
                  Dan Rathbun
                  last edited by

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

                  See: "Ruby Newbie's Guide to Getting Started"

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • Dan RathbunD Offline
                    Dan Rathbun
                    last edited by

                    @unknownuser said:

                    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.)

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • thomthomT Offline
                      thomthom
                      last edited by

                      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

                      1 Reply Last reply Reply Quote 0
                      • TIGT Offline
                        TIG Moderator
                        last edited by

                        @thomthom said:

                        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 πŸ˜’

                        TIG

                        1 Reply Last reply Reply Quote 0
                        • thomthomT Offline
                          thomthom
                          last edited by

                          @thomthom said:

                          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

                          1 Reply Last reply Reply Quote 0
                          • D Offline
                            dany67300
                            last edited by

                            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.

                            1 Reply Last reply Reply Quote 0
                            • TIGT Offline
                              TIG Moderator
                              last edited by

                              @dany67300 said:

                              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... πŸ€“

                              TIG

                              1 Reply Last reply Reply Quote 0
                              • D Offline
                                dany67300
                                last edited by

                                I hadn't seen that i could put a different material to each instance of a same defintion 😳
                                thanks a lot ! it works very well πŸ˜„

                                1 Reply Last reply Reply Quote 0
                                • B Offline
                                  bentleykfrog
                                  last edited by

                                  @dany67300 said:

                                  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?

                                  1 Reply Last reply Reply Quote 0
                                  • thomthomT Offline
                                    thomthom
                                    last edited by

                                    @bentleykfrog said:

                                    @dany67300 said:

                                    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

                                    1 Reply Last reply Reply Quote 0
                                    • S Offline
                                      sm4rt
                                      last edited by

                                      Well I got a situation !! 😲

                                      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 πŸ˜‰

                                      1 Reply Last reply Reply Quote 0
                                      • Dan RathbunD Offline
                                        Dan Rathbun
                                        last edited by

                                        The for loop should be faster, try:

                                        ` t = Time.now
                                        for i in range do

                                        code here

                                        end
                                        puts Time.now - t`

                                        I'm not here much anymore.

                                        1 Reply Last reply Reply Quote 0
                                        • S Offline
                                          sm4rt
                                          last edited by

                                          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...

                                          1 Reply Last reply Reply Quote 0
                                          • AdamBA Offline
                                            AdamB
                                            last edited by

                                            Here's another to look out for. There is a (time) cost associated with "creating" a variable, so its often faster to use variables declared outside the scope of the executing block.

                                            def doit
                                            	
                                                    start = Time.now
                                            	10000.times {
                                            		c = 5
                                            		d = 5
                                            		
                                            		e = c + d
                                            	}
                                            	puts Time.now - start
                                            	
                                            	a = 0
                                            	b = 0
                                            	c = 0
                                            	start = Time.now
                                            	10000.times {
                                            		a = 5
                                            		b = 5
                                            		
                                            		c = a + b
                                            	}
                                            	puts Time.now - start
                                            
                                            end
                                            

                                            Developer of LightUp Click for website

                                            1 Reply Last reply Reply Quote 0
                                            • 1
                                            • 2
                                            • 3
                                            • 4
                                            • 5
                                            • 6
                                            • 4 / 6
                                            • First post
                                              Last post
                                            Buy SketchPlus
                                            Buy SUbD
                                            Buy WrapR
                                            Buy eBook
                                            Buy Modelur
                                            Buy Vertex Tools
                                            Buy SketchCuisine
                                            Buy FormFonts

                                            Advertisement