Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. which one is faster and better coding

which one is faster and better coding

Scheduled Pinned Locked Moved The Weird and The Wonderful
com
19 Posts 17 Posters 11 Views 1 Watching
  • 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.
  • S Sylvester george

    First code snippet for(int i=1;i<=100;i++) DropDownList1.Items.Add(new ListItem(i.ToString(),i.ToString())); Second Code Snippet DropDownList1.Items.Add(new ListItem("1","1")); DropDownList1.Items.Add(new ListItem("2","2")); DropDownList1.Items.Add(new ListItem("3","3")); DropDownList1.Items.Add(new ListItem("4","4")); DropDownList1.Items.Add(new ListItem("5","5")); DropDownList1.Items.Add(new ListItem("6","6")); DropDownList1.Items.Add(new ListItem("7","7")); DropDownList1.Items.Add(new ListItem("8","8")); . . . . DropDownList1.Items.Add(new ListItem("99","99")); DropDownList1.Items.Add(new ListItem("100","100"));

    Regards, Sylvester G sylvester_g_m@yahoo.com

    M Offline
    M Offline
    Maximilien
    wrote on last edited by
    #2

    not really the right forum for this. but. methink the first alternative is the simplest, the fasted and can be the easiest to maintain in the long run.


    Maximilien Lincourt Your Head A Splode - Strong Bad

    1 Reply Last reply
    0
    • S Sylvester george

      First code snippet for(int i=1;i<=100;i++) DropDownList1.Items.Add(new ListItem(i.ToString(),i.ToString())); Second Code Snippet DropDownList1.Items.Add(new ListItem("1","1")); DropDownList1.Items.Add(new ListItem("2","2")); DropDownList1.Items.Add(new ListItem("3","3")); DropDownList1.Items.Add(new ListItem("4","4")); DropDownList1.Items.Add(new ListItem("5","5")); DropDownList1.Items.Add(new ListItem("6","6")); DropDownList1.Items.Add(new ListItem("7","7")); DropDownList1.Items.Add(new ListItem("8","8")); . . . . DropDownList1.Items.Add(new ListItem("99","99")); DropDownList1.Items.Add(new ListItem("100","100"));

      Regards, Sylvester G sylvester_g_m@yahoo.com

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #3

      If you'd posted the second as an example of what somebody has done then I would say this was a perfect coding horror.

      Deja View - the feeling that you've seen this post before.

      S 1 Reply Last reply
      0
      • P Pete OHanlon

        If you'd posted the second as an example of what somebody has done then I would say this was a perfect coding horror.

        Deja View - the feeling that you've seen this post before.

        S Offline
        S Offline
        Sylvester george
        wrote on last edited by
        #4

        It happened long time back with one of my friend, He was working in a company and his colleague wrote all hundred lines of code to populate drop down listbox.

        Regards, Sylvester G sylvester_g_m@yahoo.com

        B V J 3 Replies Last reply
        0
        • S Sylvester george

          First code snippet for(int i=1;i<=100;i++) DropDownList1.Items.Add(new ListItem(i.ToString(),i.ToString())); Second Code Snippet DropDownList1.Items.Add(new ListItem("1","1")); DropDownList1.Items.Add(new ListItem("2","2")); DropDownList1.Items.Add(new ListItem("3","3")); DropDownList1.Items.Add(new ListItem("4","4")); DropDownList1.Items.Add(new ListItem("5","5")); DropDownList1.Items.Add(new ListItem("6","6")); DropDownList1.Items.Add(new ListItem("7","7")); DropDownList1.Items.Add(new ListItem("8","8")); . . . . DropDownList1.Items.Add(new ListItem("99","99")); DropDownList1.Items.Add(new ListItem("100","100"));

          Regards, Sylvester G sylvester_g_m@yahoo.com

          C Offline
          C Offline
          ChandraRam
          wrote on last edited by
          #5

          The first one is certainly slower, since the compiler will have to evaluate two additional commands for every loop... but is certainly better in terms of maintainability.

          1 Reply Last reply
          0
          • S Sylvester george

            It happened long time back with one of my friend, He was working in a company and his colleague wrote all hundred lines of code to populate drop down listbox.

            Regards, Sylvester G sylvester_g_m@yahoo.com

            B Offline
            B Offline
            Brady Kelly
            wrote on last edited by
            #6

            I can see how this could be done in a script language, where there is no benefit to keeping the data outside of the application, i.e. actually use the multiple statements to define the content of the list.

            1 Reply Last reply
            0
            • S Sylvester george

              First code snippet for(int i=1;i<=100;i++) DropDownList1.Items.Add(new ListItem(i.ToString(),i.ToString())); Second Code Snippet DropDownList1.Items.Add(new ListItem("1","1")); DropDownList1.Items.Add(new ListItem("2","2")); DropDownList1.Items.Add(new ListItem("3","3")); DropDownList1.Items.Add(new ListItem("4","4")); DropDownList1.Items.Add(new ListItem("5","5")); DropDownList1.Items.Add(new ListItem("6","6")); DropDownList1.Items.Add(new ListItem("7","7")); DropDownList1.Items.Add(new ListItem("8","8")); . . . . DropDownList1.Items.Add(new ListItem("99","99")); DropDownList1.Items.Add(new ListItem("100","100"));

              Regards, Sylvester G sylvester_g_m@yahoo.com

              V Offline
              V Offline
              V 0
              wrote on last edited by
              #7

              The second one is faster I think (with a couple of µs that is) The first one is far, far better.

              V. I found a living worth working for, but haven't found work worth living for.

              1 Reply Last reply
              0
              • S Sylvester george

                First code snippet for(int i=1;i<=100;i++) DropDownList1.Items.Add(new ListItem(i.ToString(),i.ToString())); Second Code Snippet DropDownList1.Items.Add(new ListItem("1","1")); DropDownList1.Items.Add(new ListItem("2","2")); DropDownList1.Items.Add(new ListItem("3","3")); DropDownList1.Items.Add(new ListItem("4","4")); DropDownList1.Items.Add(new ListItem("5","5")); DropDownList1.Items.Add(new ListItem("6","6")); DropDownList1.Items.Add(new ListItem("7","7")); DropDownList1.Items.Add(new ListItem("8","8")); . . . . DropDownList1.Items.Add(new ListItem("99","99")); DropDownList1.Items.Add(new ListItem("100","100"));

                Regards, Sylvester G sylvester_g_m@yahoo.com

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #8

                Any improvement in speed the second may have pales in comparison to the cost in maintainability. Any why have both values be strings? And why have a DropDownList simply to select a number? Use a NumericUpDown or a TextBox with validation. I really dislike Websites that have a list of (U.S.) state abbreviations or whatnot when it's simpler just to type in the data... e.g. I now live in Arizona, but rather than allowing me to enter "AZ" (two keystrokes) I need to either scroll down or press "A" five times! Just so the programmer doesn't have to validate the entry, pathetic.

                1 Reply Last reply
                0
                • S Sylvester george

                  First code snippet for(int i=1;i<=100;i++) DropDownList1.Items.Add(new ListItem(i.ToString(),i.ToString())); Second Code Snippet DropDownList1.Items.Add(new ListItem("1","1")); DropDownList1.Items.Add(new ListItem("2","2")); DropDownList1.Items.Add(new ListItem("3","3")); DropDownList1.Items.Add(new ListItem("4","4")); DropDownList1.Items.Add(new ListItem("5","5")); DropDownList1.Items.Add(new ListItem("6","6")); DropDownList1.Items.Add(new ListItem("7","7")); DropDownList1.Items.Add(new ListItem("8","8")); . . . . DropDownList1.Items.Add(new ListItem("99","99")); DropDownList1.Items.Add(new ListItem("100","100"));

                  Regards, Sylvester G sylvester_g_m@yahoo.com

                  N Offline
                  N Offline
                  Nemanja Trifunovic
                  wrote on last edited by
                  #9

                  sylvesterg wrote:

                  which one is faster

                  It is easy to check: just make a loop with a couple of million items to add, and then type the second version up to the same number, and you'll be able to measure it with a stopwatch.


                  Programming Blog utf8-cpp

                  1 Reply Last reply
                  0
                  • S Sylvester george

                    First code snippet for(int i=1;i<=100;i++) DropDownList1.Items.Add(new ListItem(i.ToString(),i.ToString())); Second Code Snippet DropDownList1.Items.Add(new ListItem("1","1")); DropDownList1.Items.Add(new ListItem("2","2")); DropDownList1.Items.Add(new ListItem("3","3")); DropDownList1.Items.Add(new ListItem("4","4")); DropDownList1.Items.Add(new ListItem("5","5")); DropDownList1.Items.Add(new ListItem("6","6")); DropDownList1.Items.Add(new ListItem("7","7")); DropDownList1.Items.Add(new ListItem("8","8")); . . . . DropDownList1.Items.Add(new ListItem("99","99")); DropDownList1.Items.Add(new ListItem("100","100"));

                    Regards, Sylvester G sylvester_g_m@yahoo.com

                    R Offline
                    R Offline
                    rziak
                    wrote on last edited by
                    #10

                    At certain number of items the first one starts being faster because it's the same code from the cache repeated while the second one needs to load code to cache as it progresses. Better coding - for number of loops >=3 is example #1 and for number of loops < 3 is example 2. Roman Ziak www.dipmicro.com

                    1 Reply Last reply
                    0
                    • S Sylvester george

                      First code snippet for(int i=1;i<=100;i++) DropDownList1.Items.Add(new ListItem(i.ToString(),i.ToString())); Second Code Snippet DropDownList1.Items.Add(new ListItem("1","1")); DropDownList1.Items.Add(new ListItem("2","2")); DropDownList1.Items.Add(new ListItem("3","3")); DropDownList1.Items.Add(new ListItem("4","4")); DropDownList1.Items.Add(new ListItem("5","5")); DropDownList1.Items.Add(new ListItem("6","6")); DropDownList1.Items.Add(new ListItem("7","7")); DropDownList1.Items.Add(new ListItem("8","8")); . . . . DropDownList1.Items.Add(new ListItem("99","99")); DropDownList1.Items.Add(new ListItem("100","100"));

                      Regards, Sylvester G sylvester_g_m@yahoo.com

                      J Offline
                      J Offline
                      Juan Pablo G C
                      wrote on last edited by
                      #11

                      The best is use make an array with the strings and after use items.AddRange, and also for Painting part.

                      Juan Pablo G.C. Overrider Blog

                      1 Reply Last reply
                      0
                      • S Sylvester george

                        It happened long time back with one of my friend, He was working in a company and his colleague wrote all hundred lines of code to populate drop down listbox.

                        Regards, Sylvester G sylvester_g_m@yahoo.com

                        V Offline
                        V Offline
                        Vasudevan Deepak Kumar
                        wrote on last edited by
                        #12

                        sylvesterg wrote:

                        wrote all hundred lines of code to populate drop down listbox.

                        And if that is JavaScript, the amount of page size (ViewSource) that travels down the Internet pipe is really phenomenal.

                        Vasudevan Deepak Kumar Personal Homepage Tech Gossips

                        1 Reply Last reply
                        0
                        • S Sylvester george

                          First code snippet for(int i=1;i<=100;i++) DropDownList1.Items.Add(new ListItem(i.ToString(),i.ToString())); Second Code Snippet DropDownList1.Items.Add(new ListItem("1","1")); DropDownList1.Items.Add(new ListItem("2","2")); DropDownList1.Items.Add(new ListItem("3","3")); DropDownList1.Items.Add(new ListItem("4","4")); DropDownList1.Items.Add(new ListItem("5","5")); DropDownList1.Items.Add(new ListItem("6","6")); DropDownList1.Items.Add(new ListItem("7","7")); DropDownList1.Items.Add(new ListItem("8","8")); . . . . DropDownList1.Items.Add(new ListItem("99","99")); DropDownList1.Items.Add(new ListItem("100","100"));

                          Regards, Sylvester G sylvester_g_m@yahoo.com

                          M Offline
                          M Offline
                          maz2331
                          wrote on last edited by
                          #13

                          I'd go with the first as being better. Besides, the overhead in "DropDownList1.Items.Add()" itself is enough to mask any possible speed difference between the two constructs. Really, both should execute fast enough that you won't notice it anyway. Speed diff can be so easily lost due to random interrupts and kernel scheduling variations that it becomes a "who cares" issue for the most part. And the explicit one... what a nightmare to maintain.

                          1 Reply Last reply
                          0
                          • S Sylvester george

                            First code snippet for(int i=1;i<=100;i++) DropDownList1.Items.Add(new ListItem(i.ToString(),i.ToString())); Second Code Snippet DropDownList1.Items.Add(new ListItem("1","1")); DropDownList1.Items.Add(new ListItem("2","2")); DropDownList1.Items.Add(new ListItem("3","3")); DropDownList1.Items.Add(new ListItem("4","4")); DropDownList1.Items.Add(new ListItem("5","5")); DropDownList1.Items.Add(new ListItem("6","6")); DropDownList1.Items.Add(new ListItem("7","7")); DropDownList1.Items.Add(new ListItem("8","8")); . . . . DropDownList1.Items.Add(new ListItem("99","99")); DropDownList1.Items.Add(new ListItem("100","100"));

                            Regards, Sylvester G sylvester_g_m@yahoo.com

                            K Offline
                            K Offline
                            KarstenK
                            wrote on last edited by
                            #14

                            The first is the better because it is more readable and bug-safer. In the second you have to paste and copy too much. I would write String s;//here for(int i=1;i<=100;i++) { const String s = i.ToString();//could be best, so let the compiler do the optimzing !!! DropDownList1.Items.Add(new ListItem(s,s)); } so I win one call The first one is the best for the programmer - so what second :suss:

                            Greetings from Germany

                            1 Reply Last reply
                            0
                            • S Sylvester george

                              First code snippet for(int i=1;i<=100;i++) DropDownList1.Items.Add(new ListItem(i.ToString(),i.ToString())); Second Code Snippet DropDownList1.Items.Add(new ListItem("1","1")); DropDownList1.Items.Add(new ListItem("2","2")); DropDownList1.Items.Add(new ListItem("3","3")); DropDownList1.Items.Add(new ListItem("4","4")); DropDownList1.Items.Add(new ListItem("5","5")); DropDownList1.Items.Add(new ListItem("6","6")); DropDownList1.Items.Add(new ListItem("7","7")); DropDownList1.Items.Add(new ListItem("8","8")); . . . . DropDownList1.Items.Add(new ListItem("99","99")); DropDownList1.Items.Add(new ListItem("100","100"));

                              Regards, Sylvester G sylvester_g_m@yahoo.com

                              M Offline
                              M Offline
                              Mark Focas
                              wrote on last edited by
                              #15

                              Would it be better to do the call to i.ToString() only once and assign it to a variable?

                              Being in a minority of one, doesn't make you insane
                              George Orwell
                              However, in my case it does

                              1 Reply Last reply
                              0
                              • S Sylvester george

                                First code snippet for(int i=1;i<=100;i++) DropDownList1.Items.Add(new ListItem(i.ToString(),i.ToString())); Second Code Snippet DropDownList1.Items.Add(new ListItem("1","1")); DropDownList1.Items.Add(new ListItem("2","2")); DropDownList1.Items.Add(new ListItem("3","3")); DropDownList1.Items.Add(new ListItem("4","4")); DropDownList1.Items.Add(new ListItem("5","5")); DropDownList1.Items.Add(new ListItem("6","6")); DropDownList1.Items.Add(new ListItem("7","7")); DropDownList1.Items.Add(new ListItem("8","8")); . . . . DropDownList1.Items.Add(new ListItem("99","99")); DropDownList1.Items.Add(new ListItem("100","100"));

                                Regards, Sylvester G sylvester_g_m@yahoo.com

                                V Offline
                                V Offline
                                Vasudevan Deepak Kumar
                                wrote on last edited by
                                #16

                                The latter method is somewhat like C++ inline qualifier for function right?

                                Vasudevan Deepak Kumar Personal Homepage Tech Gossips

                                1 Reply Last reply
                                0
                                • S Sylvester george

                                  It happened long time back with one of my friend, He was working in a company and his colleague wrote all hundred lines of code to populate drop down listbox.

                                  Regards, Sylvester G sylvester_g_m@yahoo.com

                                  J Offline
                                  J Offline
                                  jonavi
                                  wrote on last edited by
                                  #17

                                  The first one is much better coding. The second one I would predict to be a nanosecond or something faster. The reason being the first one has to create a loop, some tostrings, an int etc.... And it still creates the same ammount of listitems, and such. The second on would make a larger dll, but probably execute a smidget faster cause of less resources..... but.... NEVER EVER DO THAT unless you have like 2 items or something small and ridicuous. The performance difference is not even close to being worth the pain of working on code like that. So... for(...) wins hands down.

                                  http://www.jonavi.com

                                  D 1 Reply Last reply
                                  0
                                  • S Sylvester george

                                    First code snippet for(int i=1;i<=100;i++) DropDownList1.Items.Add(new ListItem(i.ToString(),i.ToString())); Second Code Snippet DropDownList1.Items.Add(new ListItem("1","1")); DropDownList1.Items.Add(new ListItem("2","2")); DropDownList1.Items.Add(new ListItem("3","3")); DropDownList1.Items.Add(new ListItem("4","4")); DropDownList1.Items.Add(new ListItem("5","5")); DropDownList1.Items.Add(new ListItem("6","6")); DropDownList1.Items.Add(new ListItem("7","7")); DropDownList1.Items.Add(new ListItem("8","8")); . . . . DropDownList1.Items.Add(new ListItem("99","99")); DropDownList1.Items.Add(new ListItem("100","100"));

                                    Regards, Sylvester G sylvester_g_m@yahoo.com

                                    J Offline
                                    J Offline
                                    John R Shaw
                                    wrote on last edited by
                                    #18

                                    First, you are not supposed to ask questions here. Now, on modern day machines it does not matter that much. The second would be faster, at least it use to be and is called unrolling. In the old days the difference would be noticeable by the user and is why unrolling loops was sometime needed, especially in graphics applications. In this case the new operation is more of a bottle neck than the loop, but pre-allocating memory space can help solve that problem. Any way, Just use the loop form, as it is easier to maintain and you probably do not have a legitimate reason not to.

                                    INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

                                    1 Reply Last reply
                                    0
                                    • J jonavi

                                      The first one is much better coding. The second one I would predict to be a nanosecond or something faster. The reason being the first one has to create a loop, some tostrings, an int etc.... And it still creates the same ammount of listitems, and such. The second on would make a larger dll, but probably execute a smidget faster cause of less resources..... but.... NEVER EVER DO THAT unless you have like 2 items or something small and ridicuous. The performance difference is not even close to being worth the pain of working on code like that. So... for(...) wins hands down.

                                      http://www.jonavi.com

                                      D Offline
                                      D Offline
                                      davidnr
                                      wrote on last edited by
                                      #19

                                      There's usually an unroll-loop option in most compilers.

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups