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. General Programming
  3. Algorithms
  4. which one would be faster?

which one would be faster?

Scheduled Pinned Locked Moved Algorithms
question
14 Posts 9 Posters 6 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.
  • M Mushtaque Nizamani

    There are two sets one set has ten thousands elements other set has 30 element Which loop would be the faster for(i = 0; i < sizeoffirstset(10000); i++) { for (j=0; i < sizeofsecondset(20); j++) { if(set1[i] == set2[j]) //do some thing } } for(i = 0; i < sizeofsecondset(20); i++) { for (j=0; i < sizeoffirstset(10000); j++) { if(set2[i] == set1[j]) //do some thing } }

    Best Regards, Mushq Mushtaque Ahmed Nizamani Software Engineer Ultimus Pakistan

    R Offline
    R Offline
    Russell
    wrote on last edited by
    #4

    Mushq wrote:

    for(i = 0; i < sizeoffirstset(10000); i++) { for (j=0; i < sizeofsecondset(20); j++)

    Ahahah:laugh::laugh: faster yes .... but to go where?:laugh:


    Russell

    1 Reply Last reply
    0
    • M Mushtaque Nizamani

      There are two sets one set has ten thousands elements other set has 30 element Which loop would be the faster for(i = 0; i < sizeoffirstset(10000); i++) { for (j=0; i < sizeofsecondset(20); j++) { if(set1[i] == set2[j]) //do some thing } } for(i = 0; i < sizeofsecondset(20); i++) { for (j=0; i < sizeoffirstset(10000); j++) { if(set2[i] == set1[j]) //do some thing } }

      Best Regards, Mushq Mushtaque Ahmed Nizamani Software Engineer Ultimus Pakistan

      R Offline
      R Offline
      rihdus
      wrote on last edited by
      #5

      First one is faster and because of internal smaller array. run following code in VB: Dim i, j As Integer Dim strArr1(10000) As String Dim strArr2(20) As String For i = 0 To 10000 strArr1(i) = i & "" Next For i = 0 To 20 strArr2(i) = i * i * i & "" Next Response.Write(DateTime.Now.Millisecond.ToString()) For i = 0 To 10000 For j = 0 To 20 If (strArr1(i) = strArr2(j)) Then Response.Write("
      Hello" & strArr1(i) & strArr2(j)) End If Next Next Response.Write(DateTime.Now.Millisecond.ToString()) For i = 0 To 20 For j = 0 To 10000 If (strArr2(i) = strArr1(j)) Then Response.Write("
      Hello " & strArr2(i) & strArr1(j)) End If Next Next Response.Write(DateTime.Now.Millisecond.ToString())

      Any systematic work reflects its significance for a long time. So let's discuss the best...

      R M 2 Replies Last reply
      0
      • R rihdus

        First one is faster and because of internal smaller array. run following code in VB: Dim i, j As Integer Dim strArr1(10000) As String Dim strArr2(20) As String For i = 0 To 10000 strArr1(i) = i & "" Next For i = 0 To 20 strArr2(i) = i * i * i & "" Next Response.Write(DateTime.Now.Millisecond.ToString()) For i = 0 To 10000 For j = 0 To 20 If (strArr1(i) = strArr2(j)) Then Response.Write("
        Hello" & strArr1(i) & strArr2(j)) End If Next Next Response.Write(DateTime.Now.Millisecond.ToString()) For i = 0 To 20 For j = 0 To 10000 If (strArr2(i) = strArr1(j)) Then Response.Write("
        Hello " & strArr2(i) & strArr1(j)) End If Next Next Response.Write(DateTime.Now.Millisecond.ToString())

        Any systematic work reflects its significance for a long time. So let's discuss the best...

        R Offline
        R Offline
        Russell
        wrote on last edited by
        #6

        Yes: finally the best way to find out who is the faster is use ... the clock:laugh: :cool:


        Russell

        L 1 Reply Last reply
        0
        • R rihdus

          First one is faster and because of internal smaller array. run following code in VB: Dim i, j As Integer Dim strArr1(10000) As String Dim strArr2(20) As String For i = 0 To 10000 strArr1(i) = i & "" Next For i = 0 To 20 strArr2(i) = i * i * i & "" Next Response.Write(DateTime.Now.Millisecond.ToString()) For i = 0 To 10000 For j = 0 To 20 If (strArr1(i) = strArr2(j)) Then Response.Write("
          Hello" & strArr1(i) & strArr2(j)) End If Next Next Response.Write(DateTime.Now.Millisecond.ToString()) For i = 0 To 20 For j = 0 To 10000 If (strArr2(i) = strArr1(j)) Then Response.Write("
          Hello " & strArr2(i) & strArr1(j)) End If Next Next Response.Write(DateTime.Now.Millisecond.ToString())

          Any systematic work reflects its significance for a long time. So let's discuss the best...

          M Offline
          M Offline
          Mushtaque Nizamani
          wrote on last edited by
          #7

          Thanks for reply.

          rihdus wrote:

          First one is faster and because of internal smaller array.

          can you please explain a bit more.

          Best Regards, Mushq Mushtaque Ahmed Nizamani Software Engineer Ultimus Pakistan

          C 1 Reply Last reply
          0
          • M Mushtaque Nizamani

            Thanks for reply.

            rihdus wrote:

            First one is faster and because of internal smaller array.

            can you please explain a bit more.

            Best Regards, Mushq Mushtaque Ahmed Nizamani Software Engineer Ultimus Pakistan

            C Offline
            C Offline
            chandu004
            wrote on last edited by
            #8

            clock_t start,end; start=clock(); //your code snippet here end=clock(); //observe start=end here. hope you got the point.

            1 Reply Last reply
            0
            • M Mushtaque Nizamani

              There are two sets one set has ten thousands elements other set has 30 element Which loop would be the faster for(i = 0; i < sizeoffirstset(10000); i++) { for (j=0; i < sizeofsecondset(20); j++) { if(set1[i] == set2[j]) //do some thing } } for(i = 0; i < sizeofsecondset(20); i++) { for (j=0; i < sizeoffirstset(10000); j++) { if(set2[i] == set1[j]) //do some thing } }

              Best Regards, Mushq Mushtaque Ahmed Nizamani Software Engineer Ultimus Pakistan

              C Offline
              C Offline
              cp9876
              wrote on last edited by
              #9

              If time is important you could almost certainly do better than either way. For example, instead doing a simple search 10000 times on a set of 20 things, spend a bit of time and either order them or hash them, so that the search that you have to do many times is much faster.


              Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

              1 Reply Last reply
              0
              • M Mushtaque Nizamani

                There are two sets one set has ten thousands elements other set has 30 element Which loop would be the faster for(i = 0; i < sizeoffirstset(10000); i++) { for (j=0; i < sizeofsecondset(20); j++) { if(set1[i] == set2[j]) //do some thing } } for(i = 0; i < sizeofsecondset(20); i++) { for (j=0; i < sizeoffirstset(10000); j++) { if(set2[i] == set1[j]) //do some thing } }

                Best Regards, Mushq Mushtaque Ahmed Nizamani Software Engineer Ultimus Pakistan

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

                Mushq wrote:

                for(i = 0; i < sizeoffirstset(10000); i++) { for (j=0; i < sizeofsecondset(20); j++) { if(set1[i] == set2[j]) //do some thing } }

                another optimization:

                for(i = 0; i < sizeoffirstset(10000); i++)
                {
                const double set1_i_ = set1[i];
                for (j=0; j < sizeofsecondset(20); j++)
                {
                if(set1_i_ == set2[j])
                //do some thing
                }
                }

                In this way the program not needs to reload that value every time loooking into the array. Hoping that the compiler is enough smart to better solution itself. You can only check the time spend in every solutions and see who is the faster... Important: do this time check in Release mode!!! also look to the project features...I remember that there is some flags like smallest exe file dimension vs fastest run exe (that somethimes can be bigger)....but I don't remember the name of this flags. Hope helps:)


                Russell

                B 1 Reply Last reply
                0
                • R Russell

                  Yes: finally the best way to find out who is the faster is use ... the clock:laugh: :cool:


                  Russell

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #11

                  Sure, measuring is good, smart measuring is even better. if you have two alternatives that are likely to be almost equal, whichever you try first will take longer, due to different starting conditions (maybe code needs to be JITted, probably data needs to be loaded in memory and/or will be loaded in cache, etc). Remedy: put everything in a for loop, and run a few passes; ignore the first pass, take the average (or the best time!) of the remaining passes. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


                  1 Reply Last reply
                  0
                  • R Russell

                    Mushq wrote:

                    for(i = 0; i < sizeoffirstset(10000); i++) { for (j=0; i < sizeofsecondset(20); j++) { if(set1[i] == set2[j]) //do some thing } }

                    another optimization:

                    for(i = 0; i < sizeoffirstset(10000); i++)
                    {
                    const double set1_i_ = set1[i];
                    for (j=0; j < sizeofsecondset(20); j++)
                    {
                    if(set1_i_ == set2[j])
                    //do some thing
                    }
                    }

                    In this way the program not needs to reload that value every time loooking into the array. Hoping that the compiler is enough smart to better solution itself. You can only check the time spend in every solutions and see who is the faster... Important: do this time check in Release mode!!! also look to the project features...I remember that there is some flags like smallest exe file dimension vs fastest run exe (that somethimes can be bigger)....but I don't remember the name of this flags. Hope helps:)


                    Russell

                    B Offline
                    B Offline
                    blackjack2150
                    wrote on last edited by
                    #12

                    _Russell_ wrote:

                    Important: do this time check in Release mode!!!

                    Could you please explain a bit what the difference is? Thanks.

                    R 1 Reply Last reply
                    0
                    • B blackjack2150

                      _Russell_ wrote:

                      Important: do this time check in Release mode!!!

                      Could you please explain a bit what the difference is? Thanks.

                      R Offline
                      R Offline
                      Russell
                      wrote on last edited by
                      #13

                      The difference is that in DEBUG mode the code is compiled exactly as you write it and it isn't optimizated. For example, inline functions are not inline and macros like ASSERT are inserted in the application. In RELEASE mode the code is optimized: inline functions will become inlined and ASSERTs are cutted from the code. In this case the code is really faster and it does exactly what you want, without do any internal test. Of course you have, first, to prepare the routines in DEBUG mode, to be sure that there isn't errors, but when you take the clock in your hand to see what algorithm is faster... then use the RELEASE mode. You can find details on the differences between DEBUG mode and RELEASE mode into the documentation. :-D


                      Russell

                      1 Reply Last reply
                      0
                      • M Mushtaque Nizamani

                        There are two sets one set has ten thousands elements other set has 30 element Which loop would be the faster for(i = 0; i < sizeoffirstset(10000); i++) { for (j=0; i < sizeofsecondset(20); j++) { if(set1[i] == set2[j]) //do some thing } } for(i = 0; i < sizeofsecondset(20); i++) { for (j=0; i < sizeoffirstset(10000); j++) { if(set2[i] == set1[j]) //do some thing } }

                        Best Regards, Mushq Mushtaque Ahmed Nizamani Software Engineer Ultimus Pakistan

                        C Offline
                        C Offline
                        Chetan Patel
                        wrote on last edited by
                        #14

                        Both are same because the no. of iterations is same.

                        Best Regards, Chetan Patel

                        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