Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.basic.visual.misc > #785

Re: What is a class?

From Helmut_Meukel <Helmut_Meukel@bn-hof.invalid>
Newsgroups comp.lang.basic.visual.misc
Subject Re: What is a class?
Date 2012-02-25 11:36 +0100
Organization HM - Soft Hof
Message-ID <jiadi6$ohs$1@dont-email.me> (permalink)
References <e20a08e0-5d12-4ec8-bb47-c8d415c71d60@hs8g2000vbb.googlegroups.com> <CQN1r.159616$M05.63778@newsfe20.ams2> <ji8pqc$a9t$1@dont-email.me> <FNU1r.86371$dZ7.43055@newsfe08.ams2>

Show all headers | View raw


Ivar wrote:
> Gone from what is a class to Looping.
> I've Written a rough bit of code below to demo the time diff between For Next 
> and For Each Next when iterating Collections
>
> My Results. About 7 seconds total time
> 4914    For Next
> 0          For Each Next
> 1950    Find By Index In Collection
> 30        Find By Key in Collection
> 0          Find By Index in Array
>

There's a principle flaw in your approach, you are timing computations
in some of the loops which /might/ increase access time unnecessarily.
See my changes in your code below.

> Option Explicit
> Private Declare Function GetTickCount& Lib "kernel32" ()
>
> Private Sub Command1_Click()
> Dim SomeVar As Variant
> Dim L As Long
> Dim TheCol As New Collection
> Dim TheArray(50000) As Long
> Dim TickNow As Long
> Dim I As Integer
> Dim Time1 As Long
> Dim Time2 As Long
> Dim Time3 As Long
> Dim Time4 As Long
> Dim Time5 As Long
> Dim S As String
>
> 'Add 50,000 Items to a collection
> For L = 1 To 50000
> TheCol.Add L, "S" & L
> Next
>
> 'Use For Next to Load Array Values (Time 1)
> TickNow = GetTickCount
> For L = 1 To 50000
> TheArray(L) = TheCol(L)
> Next
>
> Time1 = GetTickCount - TickNow
> TickNow = GetTickCount
>
> 'Use For Each Next Loop To Load Array Values (Time 2)
> L = 0
> For Each SomeVar In TheCol
> L = L + 1
> TheArray(L) = SomeVar
> Next
>
> Time2 = GetTickCount - TickNow
>

In the next three loops you are computing the index within the loop.
Why? Depending on your version of VB and probably the OS and CPU,
using Integers as loop counters may be slower than using Longs:

> 'Find 10000 items by index in Collection (Time 3)
> TickNow = GetTickCount
> For I = 1 To 10000
> L = TheCol.Item(40000 + I)
> Next
better to use a long for I:
  For I = 40001 To 50000
     L = TheCol.Item(I)
  Next

> Time3 = GetTickCount - TickNow
>
> 'Find 10000 items by Key in Collection(Time 4)
> TickNow = GetTickCount
> For I = 1 To 10000
> L = TheCol.Item("S" & 40000 + 1)
> Next
better to use a long for I:
  For I = 40001 To 50000
     L = TheCol.Item("S" & I)
  Next
In many real world apps the string key isn't computed only to access the
collection, thus  the concatination adds unnecessary time to the loop.
Storing the strings in an array (done outside of the loop) and accessing
the items by
     L = TheCol.Item(StringArray(I))
should be faster.

> Time4 = GetTickCount - TickNow
>
> 'Find 10000 items in the Array (Time 5)
> TickNow = GetTickCount
> For I = 1 To 10000
> L = TheArray(40000 + I)
> Next

again better to use a long for I:
  For I = 40001 To 50000
     L = TheArray(I)
  Next

> Time5 = GetTickCount - TickNow
>
> S = "For Next Loop Took " & Time1 & vbCrLf
> S = S & "For Each Next Took " & Time2 & vbCrLf
> S = S & "Find 10000 By Index Took " & Time3 & vbCrLf
> S = S & "Find 10000 By Key Took " & Time4 & vbCrLf
> S = S & "Find 10000 Items In Array Took  " & Time5 & vbCrLf
> MsgBox S
> End Sub

One could argue, because the 40000 + I is within all loops, it doesn't
matter, but it will change the ratio.
Say 100 ticks to 50 ticks compared to (100 + 20) ticks to (50 + 20) ticks.

OTOH, there are situations where you should include lengthy computations
into your test loop; e.g. if the items are stored to a remote database,
the speed differences of the internal storage (array vs. collection) may
be unnoticeable. :-)

Helmut.

Back to comp.lang.basic.visual.misc | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

What is a class? Peter Nolan <peter.nolan40@gmail.com> - 2012-02-24 04:02 -0800
  Re: What is a class? "Auric__" <not.my.real@email.address> - 2012-02-24 14:06 +0000
    Re: What is a class? Peter Nolan <peter.nolan40@gmail.com> - 2012-02-25 03:07 -0800
      Re: What is a class? Helmut_Meukel <Helmut_Meukel@bn-hof.invalid> - 2012-02-25 14:19 +0100
      Re: What is a class? "Auric__" <not.my.real@email.address> - 2012-02-25 21:26 +0000
        Re: What is a class? Jim Mack <no-uce-ube@mdxi.com> - 2012-02-25 18:58 -0500
          Re: What is a class? GS <gs@somewhere.net> - 2012-02-25 19:39 -0500
          Re: What is a class? "Auric__" <not.my.real@email.address> - 2012-02-26 03:09 +0000
            Re: What is a class? Jim Mack <no-uce-ube@mdxi.com> - 2012-02-25 22:35 -0500
              Re: What is a class? GS <gs@somewhere.net> - 2012-02-25 22:55 -0500
                Re: What is a class? "Farnsworth" <nospam@nospam.com> - 2012-02-25 23:27 -0500
                Re: What is a class? GS <gs@somewhere.net> - 2012-02-26 01:30 -0500
                Re: What is a class? "Auric__" <not.my.real@email.address> - 2012-02-26 04:41 +0000
                Re: What is a class? ralph <nt_consulting64@yahoo.net> - 2012-02-25 23:05 -0600
                Re: What is a class? "Henning" <computer_hero@coldmail.com> - 2012-02-26 11:24 +0100
                Re: What is a class? ralph <nt_consulting64@yahoo.net> - 2012-02-25 23:03 -0600
                Re: What is a class? GS <gs@somewhere.net> - 2012-02-26 01:26 -0500
                Re: What is a class? "Stuart McCall" <smccall@myunrealbox.com> - 2012-02-27 04:15 +0000
                Re: What is a class? ralph <nt_consulting64@yahoo.net> - 2012-02-27 00:48 -0600
                Re: What is a class? "Mayayana" <mayayana@invalid.nospam> - 2012-02-27 08:57 -0500
                Re: What is a class? "Stuart McCall" <smccall@myunrealbox.com> - 2012-02-27 19:19 +0000
                Re: What is a class? "Stuart McCall" <smccall@myunrealbox.com> - 2012-02-27 19:12 +0000
                Re: What is a class? "Bob Butler" <bob_butler@cox.invalid> - 2012-02-27 06:06 -0800
                Re: What is a class? "Stuart McCall" <smccall@myunrealbox.com> - 2012-02-27 19:24 +0000
                Re: What is a class? GS <gs@somewhere.net> - 2012-02-27 11:01 -0500
                Re: What is a class? ralph <nt_consulting64@yahoo.net> - 2012-02-27 11:07 -0600
                Re: What is a class? GS <gs@somewhere.net> - 2012-02-27 13:10 -0500
                Re: What is a class? Schmidt <sss@online.de> - 2012-02-27 19:09 +0100
                Re: What is a class? GS <gs@somewhere.net> - 2012-02-27 13:29 -0500
                Re: What is a class? GS <gs@somewhere.net> - 2012-02-27 14:43 -0500
                Re: What is a class? Schmidt <sss@online.de> - 2012-02-27 22:14 +0100
                Re: What is a class? GS <gs@somewhere.net> - 2012-02-27 16:45 -0500
                Re: What is a class? GS <gs@somewhere.net> - 2012-02-27 18:23 -0500
                Re: What is a class? GS <gs@somewhere.net> - 2012-02-27 23:26 -0500
                Re: What is a class? Schmidt <sss@online.de> - 2012-02-28 13:54 +0100
                Re: What is a class? GS <gs@somewhere.net> - 2012-02-28 08:37 -0500
                Re: What is a class? ralph <nt_consulting64@yahoo.net> - 2012-02-28 09:19 -0600
                Re: What is a class? GS <gs@somewhere.net> - 2012-02-28 10:42 -0500
                Re: What is a class? Schmidt <sss@online.de> - 2012-02-29 06:47 +0100
                Re: What is a class? GS <gs@somewhere.net> - 2012-02-29 09:39 -0500
                Re: What is a class? GS <gs@somewhere.net> - 2012-02-29 13:33 -0500
                Re: What is a class? GS <gs@somewhere.net> - 2012-02-29 14:43 -0500
                Re: What is a class? Schmidt <sss@online.de> - 2012-02-29 21:39 +0100
                Re: What is a class? GS <gs@somewhere.net> - 2012-02-29 16:15 -0500
                Re: What is a class? Schmidt <sss@online.de> - 2012-02-29 23:20 +0100
                Re: What is a class? GS <gs@somewhere.net> - 2012-02-28 00:16 -0500
                Re: What is a class? Jim Mack <no-uce-ube@mdxi.com> - 2012-02-27 13:26 -0500
                Re: What is a class? "Stuart McCall" <smccall@myunrealbox.com> - 2012-02-27 19:33 +0000
                Re: What is a class? Jim Mack <no-uce-ube@mdxi.com> - 2012-02-27 15:50 -0500
                Re: What is a class? Helmut_Meukel <Helmut_Meukel@bn-hof.invalid> - 2012-02-26 11:02 +0100
                Re: What is a class? GS <gs@somewhere.net> - 2012-02-26 14:23 -0500
                Re: What is a class? GS <gs@somewhere.net> - 2012-02-26 17:09 -0500
            Re: What is a class? ralph <nt_consulting64@yahoo.net> - 2012-02-25 22:21 -0600
              Re: What is a class? "Auric__" <not.my.real@email.address> - 2012-02-26 04:51 +0000
                Re: What is a class? ralph <nt_consulting64@yahoo.net> - 2012-02-25 23:10 -0600
        Re: What is a class? Peter Nolan <peter.nolan40@gmail.com> - 2012-02-26 04:11 -0800
          Re: What is a class? Helmut_Meukel <Helmut_Meukel@bn-hof.invalid> - 2012-02-26 16:37 +0100
          Re: What is a class? "Auric__" <not.my.real@email.address> - 2012-02-26 18:36 +0000
            Re: What is a class? ralph <nt_consulting64@yahoo.net> - 2012-02-26 14:11 -0600
            Re: What is a class? Peter Nolan <peter.nolan40@gmail.com> - 2012-02-27 03:11 -0800
              Re: What is a class? "Auric__" <not.my.real@email.address> - 2012-02-27 16:33 +0000
                Re: What is a class? Jim Mack <no-uce-ube@mdxi.com> - 2012-02-27 13:33 -0500
                Re: What is a class? Peter Nolan <peter.nolan40@gmail.com> - 2012-02-28 04:07 -0800
    Re: What is a class? Peter Nolan <peter.nolan40@gmail.com> - 2012-02-25 03:33 -0800
    Re: What is a class? Peter Nolan <peter.nolan40@gmail.com> - 2012-02-25 04:19 -0800
  Re: What is a class? "Ivar" <ivar.ekstromer000@ntlworld.com> - 2012-02-24 15:15 +0000
    Re: What is a class? GS <gs@somewhere.net> - 2012-02-24 14:52 -0500
      Re: What is a class? "Ivar" <ivar.ekstromer000@ntlworld.com> - 2012-02-24 23:10 +0000
        Re: What is a class? GS <gs@somewhere.net> - 2012-02-24 19:46 -0500
          Re: What is a class Way off Topic "Ivar" <ivar.ekstromer000@ntlworld.com> - 2012-02-25 02:20 +0000
            Re: What is a class Way off Topic GS <gs@somewhere.net> - 2012-02-25 16:57 -0500
        Re: What is a class? Helmut_Meukel <Helmut_Meukel@bn-hof.invalid> - 2012-02-25 11:36 +0100
    Re: What is a class? Peter Nolan <peter.nolan40@gmail.com> - 2012-02-25 03:35 -0800
  Re: What is a class? Jim Mack <no-uce-ube@mdxi.com> - 2012-02-24 15:23 -0500
    Re: What is a class? "Auric__" <not.my.real@email.address> - 2012-02-25 01:33 +0000
    Re: What is a class? Peter Nolan <peter.nolan40@gmail.com> - 2012-02-25 03:09 -0800
  Re: What is a class? "Mayayana" <mayayana@invalid.nospam> - 2012-02-24 20:05 -0500

csiph-web