How VB implements the Collection 
Author Message
 How VB implements the Collection

Hi,
Does anybody know how VB implements the Collection. Is it possible to write
a COM collection object using VC C++ that is faster than VB's Collection.
Thanks,
-Tony


Sat, 11 Sep 2004 13:56:54 GMT  
 How VB implements the Collection

Quote:

> Hi,
> Does anybody know how VB implements the Collection. Is it possible to write
> a COM collection object using VC C++ that is faster than VB's Collection.
> Thanks,
> -Tony

As far as I know VB implements the Collection in C++ code in the
runtime using a hashing algorithm.  Last year I was able to implement
my "own" Collection in VB-6 using hashing, and the result ran 15%
faster than VB collections.  When my boss saw it, he reminded me that
Francesco Balena did this a number of years ago.  This shows that the
key is not the language it is a cool algorithm which I used.

There have been a number of instances where developers working purely
in VB recreate system features that run faster than the Microsoft
implementation.  A coworker heard a VSLive speaker in 1999 who'd
implemented VB-compatible math in pure VB that ran faster than the VB
runtime.

What this entails may be only that the Microsoft developers have other
fish to fry, or it may actually mean that not all good developers work
for Microsoft.

You do not have to use C++ to create a Collection object that
outperforms the Collection object provided with the system: just use a
better hashing algorithm.  Or, convert to VB.Net which has a range of
improved collections including a hash table.



Sun, 12 Sep 2004 01:10:38 GMT  
 How VB implements the Collection
So..

What is Faster?
(I need to use 'Key' search capability)

Dictionary or collection?

Thanks!




Quote:
> > Hi,
> > Does anybody know how VB implements the Collection. Is it possible to
write
> > a COM collection object using VC C++ that is faster than VB's
Collection.
> > Thanks,
> > -Tony

> As far as I know VB implements the Collection in C++ code in the
> runtime using a hashing algorithm.  Last year I was able to implement
> my "own" Collection in VB-6 using hashing, and the result ran 15%
> faster than VB collections.  When my boss saw it, he reminded me that
> Francesco Balena did this a number of years ago.  This shows that the
> key is not the language it is a cool algorithm which I used.

> There have been a number of instances where developers working purely
> in VB recreate system features that run faster than the Microsoft
> implementation.  A coworker heard a VSLive speaker in 1999 who'd
> implemented VB-compatible math in pure VB that ran faster than the VB
> runtime.

> What this entails may be only that the Microsoft developers have other
> fish to fry, or it may actually mean that not all good developers work
> for Microsoft.

> You do not have to use C++ to create a Collection object that
> outperforms the Collection object provided with the system: just use a
> better hashing algorithm.  Or, convert to VB.Net which has a range of
> improved collections including a hash table.



Sun, 12 Sep 2004 01:16:02 GMT  
 How VB implements the Collection

Quote:

> So..

> What is Faster?
> (I need to use 'Key' search capability)

> Dictionary or collection?

> Thanks!

Collection.


Sun, 12 Sep 2004 06:39:29 GMT  
 How VB implements the Collection

Quote:

> So..

> What is Faster?
> (I need to use 'Key' search capability)

> Dictionary or collection?

Using Dictionary will result in a faster trip to DLL Hell, since
Microslop seems to like to break SCRRUN.DLL every time they release
a new version of their Virus Broadcast System into the wild...  I'd
advise you to learn how to write your own Collection-like classes,
but supporting For Each without delegating to a Collection anyway
is a ridiculous PITA!  Google on "_NewEnum", but not right after
lunch.

--
Joe Foster <mailto:jlfoster%40znet.com>  Sacrament R2-45 <http://www.xenu.net/>
WARNING: I cannot be held responsible for the above        They're   coming  to
because  my cats have  apparently  learned to type.        take me away, ha ha!



Sun, 12 Sep 2004 14:55:09 GMT  
 How VB implements the Collection
     I'm no expert on this but it's something I've been looking into lately.
According to Paul Lomax in VB & VBA in a Nutshell, a Dictionary is
2 to 3 times faster than a collection, depending on what you're doing.
It's also easier to work with because you don't need to remove and re-add
a key just to change the value.
  On the downside you have to ship SCRRUN.DLL, which needs
MSVCRT.DLL, adding about 400 KB to the package.

  Does anyone have an opinion about using a custom data type
instead of a collection? Is there a limit to the number of members a data
type can hold?

--
--

Quote:
> So..

> What is Faster?
> (I need to use 'Key' search capability)

> Dictionary or collection?

> Thanks!





> > > Hi,
> > > Does anybody know how VB implements the Collection. Is it possible to
> write
> > > a COM collection object using VC C++ that is faster than VB's
> Collection.
> > > Thanks,
> > > -Tony

> > As far as I know VB implements the Collection in C++ code in the
> > runtime using a hashing algorithm.  Last year I was able to implement
> > my "own" Collection in VB-6 using hashing, and the result ran 15%
> > faster than VB collections.  When my boss saw it, he reminded me that
> > Francesco Balena did this a number of years ago.  This shows that the
> > key is not the language it is a cool algorithm which I used.

> > There have been a number of instances where developers working purely
> > in VB recreate system features that run faster than the Microsoft
> > implementation.  A coworker heard a VSLive speaker in 1999 who'd
> > implemented VB-compatible math in pure VB that ran faster than the VB
> > runtime.

> > What this entails may be only that the Microsoft developers have other
> > fish to fry, or it may actually mean that not all good developers work
> > for Microsoft.

> > You do not have to use C++ to create a Collection object that
> > outperforms the Collection object provided with the system: just use a
> > better hashing algorithm.  Or, convert to VB.Net which has a range of
> > improved collections including a hash table.



Mon, 13 Sep 2004 02:00:00 GMT  
 How VB implements the Collection

Quote:

> I'm no expert on this but it's something I've been looking into lately.
> According to Paul Lomax in VB & VBA in a Nutshell, a Dictionary is
> 2 to 3 times faster than a collection, depending on what you're doing.
> It's also easier to work with because you don't need to remove and re-add
> a key just to change the value.
>   On the downside you have to ship SCRRUN.DLL, which needs
> MSVCRT.DLL, adding about 400 KB to the package.

>   Does anyone have an opinion about using a custom data type
> instead of a collection? Is there a limit to the number of members a data
> type can hold?

I'm unclear on the question.  I really hope you do not plan on
implementing something like this:

     Private Type TYPhorribleType
          strMember1 As String                  ' Eeek
          strMember2 As String                  ' Yow
          strMember3 As String                  ' No
          strMember4 As String                  ' Aieee
          .
          .
          .
          strMember999 As String                ' Blarg
     End Type

(I've seen horrors like this, often justified by appeal to corporate
"needs", broadly construed, the need for people to act like idiots.)

Perhaps you mean something like this

     Private Type TYPcollectionType
         strMember() As String
     End Type

that is, an array dynamically managed by the collection.  Here there
is no apriori limit on the size.

However, you can always create a non-collection user data type with
the fields you want (such as name, address, city, state and zip) and
then assign elements of this type to a straight collection.  This is
the best way to implement collections of typed objects.

Quote:

> --
> --


> > So..

> > What is Faster?
> > (I need to use 'Key' search capability)

> > Dictionary or collection?

> > Thanks!





> > > > Hi,
> > > > Does anybody know how VB implements the Collection. Is it possible to
>  write
> > > > a COM collection object using VC C++ that is faster than VB's
>  Collection.
> > > > Thanks,
> > > > -Tony

> > > As far as I know VB implements the Collection in C++ code in the
> > > runtime using a hashing algorithm.  Last year I was able to implement
> > > my "own" Collection in VB-6 using hashing, and the result ran 15%
> > > faster than VB collections.  When my boss saw it, he reminded me that
> > > Francesco Balena did this a number of years ago.  This shows that the
> > > key is not the language it is a cool algorithm which I used.

> > > There have been a number of instances where developers working purely
> > > in VB recreate system features that run faster than the Microsoft
> > > implementation.  A coworker heard a VSLive speaker in 1999 who'd
> > > implemented VB-compatible math in pure VB that ran faster than the VB
> > > runtime.

> > > What this entails may be only that the Microsoft developers have other
> > > fish to fry, or it may actually mean that not all good developers work
> > > for Microsoft.

> > > You do not have to use C++ to create a Collection object that
> > > outperforms the Collection object provided with the system: just use a
> > > better hashing algorithm.  Or, convert to VB.Net which has a range of
> > > improved collections including a hash table.



Mon, 13 Sep 2004 23:12:09 GMT  
 How VB implements the Collection
Yes, I'm afraid I was thinking of the TYPhorrible type! It did seem
like a messy idea but I've been trying to think of something more
efficient than what I've got.

   I need to hold 5 instances of about 30 values. At this point
I'm using a class that instatiates 5 collections and provides
Get and Let properties for those values. It works fine but it
seems like an awfully bloated way to do it.
   I can't use an array because I want names.
     Another idea I thought of was to just instantiate multiples of a
class that has 30 private variables. That also seems a bit messy,
 but then I reason that the Collections must be doing a similar thing
and why not cut out the middleman. Does that make sense?

--
--



Quote:
> > I'm no expert on this but it's something I've been looking into lately.
> > According to Paul Lomax in VB & VBA in a Nutshell, a Dictionary is
> > 2 to 3 times faster than a collection, depending on what you're doing.
> > It's also easier to work with because you don't need to remove and
re-add
> > a key just to change the value.
> >   On the downside you have to ship SCRRUN.DLL, which needs
> > MSVCRT.DLL, adding about 400 KB to the package.

> >   Does anyone have an opinion about using a custom data type
> > instead of a collection? Is there a limit to the number of members a
data
> > type can hold?

> I'm unclear on the question.  I really hope you do not plan on
> implementing something like this:

>      Private Type TYPhorribleType
>           strMember1 As String                  ' Eeek
>           strMember2 As String                  ' Yow
>           strMember3 As String                  ' No
>           strMember4 As String                  ' Aieee
>           .
>           .
>           .
>           strMember999 As String                ' Blarg
>      End Type

> (I've seen horrors like this, often justified by appeal to corporate
> "needs", broadly construed, the need for people to act like idiots.)

> Perhaps you mean something like this

>      Private Type TYPcollectionType
>          strMember() As String
>      End Type

> that is, an array dynamically managed by the collection.  Here there
> is no apriori limit on the size.

> However, you can always create a non-collection user data type with
> the fields you want (such as name, address, city, state and zip) and
> then assign elements of this type to a straight collection.  This is
> the best way to implement collections of typed objects.

> > --
> > --


> > > So..

> > > What is Faster?
> > > (I need to use 'Key' search capability)

> > > Dictionary or collection?

> > > Thanks!





> > > > > Hi,
> > > > > Does anybody know how VB implements the Collection. Is it possible
to
> >  write
> > > > > a COM collection object using VC C++ that is faster than VB's
> >  Collection.
> > > > > Thanks,
> > > > > -Tony

> > > > As far as I know VB implements the Collection in C++ code in the
> > > > runtime using a hashing algorithm.  Last year I was able to
implement
> > > > my "own" Collection in VB-6 using hashing, and the result ran 15%
> > > > faster than VB collections.  When my boss saw it, he reminded me
that
> > > > Francesco Balena did this a number of years ago.  This shows that
the
> > > > key is not the language it is a cool algorithm which I used.

> > > > There have been a number of instances where developers working
purely
> > > > in VB recreate system features that run faster than the Microsoft
> > > > implementation.  A coworker heard a VSLive speaker in 1999 who'd
> > > > implemented VB-compatible math in pure VB that ran faster than the
VB
> > > > runtime.

> > > > What this entails may be only that the Microsoft developers have
other
> > > > fish to fry, or it may actually mean that not all good developers
work
> > > > for Microsoft.

> > > > You do not have to use C++ to create a Collection object that
> > > > outperforms the Collection object provided with the system: just use
a
> > > > better hashing algorithm.  Or, convert to VB.Net which has a range
of
> > > > improved collections including a hash table.



Mon, 13 Sep 2004 23:54:39 GMT  
 How VB implements the Collection

Quote:

> Yes, I'm afraid I was thinking of the TYPhorrible type! It did seem
> like a messy idea but I've been trying to think of something more
> efficient than what I've got.

>    I need to hold 5 instances of about 30 values. At this point
> I'm using a class that instatiates 5 collections and provides
> Get and Let properties for those values. It works fine but it
> seems like an awfully bloated way to do it.
>    I can't use an array because I want names.
>      Another idea I thought of was to just instantiate multiples of a
> class that has 30 private variables. That also seems a bit messy,
>  but then I reason that the Collections must be doing a similar thing
> and why not cut out the middleman. Does that make sense?

To me, what you want is a two-dimensional array but with associative
indexes, rather than numbers.

The simplest way to implement this would be as a Collection.  When you
add an element to the collection, provide a key that is structured
into the key of the first dimension and the key of the second
dimension.

For example, to add value 4 of instance 2:

     colCollection.Add(value, "instance2.value4")

In the above, instance2 and value4 would be replaced by meaningful
keys, first to the instance and then to the level.

For example, if you are storing 30 values for the five Three Stooges,
Moe, Larry, Shemp, Curley and CurleyJoe, where the values are films in
which they appear, to add the fact that Curley starred in Three Blind
Mopes, use

     strStooge = "Curley": strFilm = "Three Blind Mopes"
     colCollection.Add(True, strStooge & "." & strFilm)

Note that the value is just True, but it could be facts concerning
Curley's role in the film.

In general, the design should be delinked from any considerations of
how many including the fact that you have five instances of thirty
values.  Delinking the design allows you to focus on the abstraction.

Quote:

> --
> --




> > > I'm no expert on this but it's something I've been looking into lately.
> > > According to Paul Lomax in VB & VBA in a Nutshell, a Dictionary is
> > > 2 to 3 times faster than a collection, depending on what you're doing.
> > > It's also easier to work with because you don't need to remove and
>  re-add
> > > a key just to change the value.
> > >   On the downside you have to ship SCRRUN.DLL, which needs
> > > MSVCRT.DLL, adding about 400 KB to the package.

> > >   Does anyone have an opinion about using a custom data type
> > > instead of a collection? Is there a limit to the number of members a
>  data
> > > type can hold?

> > I'm unclear on the question.  I really hope you do not plan on
> > implementing something like this:

> >      Private Type TYPhorribleType
> >           strMember1 As String                  ' Eeek
> >           strMember2 As String                  ' Yow
> >           strMember3 As String                  ' No
> >           strMember4 As String                  ' Aieee
> >           .
> >           .
> >           .
> >           strMember999 As String                ' Blarg
> >      End Type

> > (I've seen horrors like this, often justified by appeal to corporate
> > "needs", broadly construed, the need for people to act like idiots.)

> > Perhaps you mean something like this

> >      Private Type TYPcollectionType
> >          strMember() As String
> >      End Type

> > that is, an array dynamically managed by the collection.  Here there
> > is no apriori limit on the size.

> > However, you can always create a non-collection user data type with
> > the fields you want (such as name, address, city, state and zip) and
> > then assign elements of this type to a straight collection.  This is
> > the best way to implement collections of typed objects.

> > > --
> > > --


> > > > So..

> > > > What is Faster?
> > > > (I need to use 'Key' search capability)

> > > > Dictionary or collection?

> > > > Thanks!





> > > > > > Hi,
> > > > > > Does anybody know how VB implements the Collection. Is it possible
>  to
>  write
> > > > > > a COM collection object using VC C++ that is faster than VB's
>  Collection.
> > > > > > Thanks,
> > > > > > -Tony

> > > > > As far as I know VB implements the Collection in C++ code in the
> > > > > runtime using a hashing algorithm.  Last year I was able to
>  implement
> > > > > my "own" Collection in VB-6 using hashing, and the result ran 15%
> > > > > faster than VB collections.  When my boss saw it, he reminded me
>  that
> > > > > Francesco Balena did this a number of years ago.  This shows that
>  the
> > > > > key is not the language it is a cool algorithm which I used.

> > > > > There have been a number of instances where developers working
>  purely
> > > > > in VB recreate system features that run faster than the Microsoft
> > > > > implementation.  A coworker heard a VSLive speaker in 1999 who'd
> > > > > implemented VB-compatible math in pure VB that ran faster than the
>  VB
> > > > > runtime.

> > > > > What this entails may be only that the Microsoft developers have
>  other
> > > > > fish to fry, or it may actually mean that not all good developers
>  work
> > > > > for Microsoft.

> > > > > You do not have to use C++ to create a Collection object that
> > > > > outperforms the Collection object provided with the system: just use
>  a
> > > > > better hashing algorithm.  Or, convert to VB.Net which has a range
>  of
> > > > > improved collections including a hash table.



Tue, 14 Sep 2004 03:28:22 GMT  
 How VB implements the Collection

Quote:

> Yes, I'm afraid I was thinking of the TYPhorrible type! It did seem
> like a messy idea but I've been trying to think of something more
> efficient than what I've got.

>    I need to hold 5 instances of about 30 values. At this point
> I'm using a class that instatiates 5 collections and provides
> Get and Let properties for those values. It works fine but it
> seems like an awfully bloated way to do it.
>    I can't use an array because I want names.

Use an Enum to hold the names, perhaps something like this:

 URL:http://groups.google.com/groups?selm=ORaIm8j1BHA.2212%40tkmsftngp05

--
Joe Foster <mailto:jlfoster%40znet.com>  Sign the Check! <http://www.xenu.net/>
WARNING: I cannot be held responsible for the above        They're   coming  to
because  my cats have  apparently  learned to type.        take me away, ha ha!



Tue, 14 Sep 2004 04:22:46 GMT  
 How VB implements the Collection
Thank you. That's an interesting design. I gather, then
that using variables in a class IS more efficient than
a collection? I don't have a grasp of what difference there might
be in how the memory is handled but assumed that a collection
has to be at least as indirect, and at least as memory intensive,
as variables accessed through a class.

--
--



Quote:

> > Yes, I'm afraid I was thinking of the TYPhorrible type! It did seem
> > like a messy idea but I've been trying to think of something more
> > efficient than what I've got.

> >    I need to hold 5 instances of about 30 values. At this point
> > I'm using a class that instatiates 5 collections and provides
> > Get and Let properties for those values. It works fine but it
> > seems like an awfully bloated way to do it.
> >    I can't use an array because I want names.

> Use an Enum to hold the names, perhaps something like this:

>  URL:http://groups.google.com/groups?selm=ORaIm8j1BHA.2212%40tkmsftngp05

> --
> Joe Foster <mailto:jlfoster%40znet.com>  Sign the Check!

<http://www.xenu.net/>

- Show quoted text -

Quote:
> WARNING: I cannot be held responsible for the above        They're
coming  to
> because  my cats have  apparently  learned to type.        take me away,
ha ha!



Tue, 14 Sep 2004 12:51:01 GMT  
 How VB implements the Collection

Quote:

> Thank you. That's an interesting design. I gather, then
> that using variables in a class IS more efficient than
> a collection? I don't have a grasp of what difference there might
> be in how the memory is handled but assumed that a collection
> has to be at least as indirect, and at least as memory intensive,
> as variables accessed through a class.

Collections are pretty expensive, but it'll be faster than the
approach I used if there are a /lot/ of items/properties!  There
are ways to speed up my Items "property", though, such as using
a binary search or "minimal perfect hash function" on Index when
it's a string, so the Select Case is always done on a number,
though it's usually pointless for less than a dozen or so items:

private type luentry
  key as string
  num as long
end type

private function lookup(byref index as variant) as long
  static lu() as luentry, initialized as boolean

  if not initialized then
    redim lu(0 to 3)
    ' make sure the keys are in sorted order!
    lu(0).key = "firstname"
    lu(0).num = sffirstname
    lu(1).key = "id"
    lu(1).num = sfid
    lu(2).key = "lastname"
    lu(2).num = sflastname
    lu(3).key = "registered"
    lu(3).num = sfregistered
    initialized = true
  end if

  if vartype(index) <> vbstring then
    lookup = index
    exit function
  end if

  ' Binary search is harder than it seems -- according to Knuth,
  ' the first version was published in 1946, but the first /correct/
  ' version wasn't published until 1962, /six{*filter*} years/ later!
  dim l as long, u as long, m as long
  l = lbound(lu)
  u = ubound(lu)
  do
    m = (l + u) \ 2
    select case strcomp(index, lu(m).key, vbtextcompare)
    case is < 0
      u = m - 1
    case is > 0
      l = m + 1
    case else
      lookup = lu(m).num
      exit function
    end select
  loop while l <= u
  lookup = -1
end function

public property get fields(byref index as variant) as variant
  select case lookup(index)
  case sfid
    fields = id
  case sflastname
    fields = lastname
  case sffirstname
    fields = firstname
  case sfregistered
    fields = registered
  case else
    err.raise number:=5
  end select
end property

--
Joe Foster <mailto:jlfoster%40znet.com>     Got Thetans? < http://www.*-*-*.com/ ;
WARNING: I cannot be held responsible for the above        They're   coming  to
because  my cats have  apparently  learned to type.        take me away, ha ha!



Wed, 15 Sep 2004 01:40:11 GMT  
 How VB implements the Collection
Thanks for your help, Joe. I've never paid much attention to
Enums, frankly. As it turned out, I made a small class that just provides
access to the values of a 30-element array by index. Then a master class
creates an array of 5 of the small classes. Using a 30-member Enum
in the master class allowed for setting all 150 values using only numeric
parameters with "intellisense" for names: Class.Val(EnumName, index) = s
 That seems to work well, eliminated all Select Case and Collections, and
shaved a surprising amount off the size of the EXE file size.
  (Though I suppose there's a limit to how big one can go with Enums.)

Joe Priestley

--
--



Quote:

> > Thank you. That's an interesting design. I gather, then
> > that using variables in a class IS more efficient than
> > a collection? I don't have a grasp of what difference there might
> > be in how the memory is handled but assumed that a collection
> > has to be at least as indirect, and at least as memory intensive,
> > as variables accessed through a class.

> Collections are pretty expensive, but it'll be faster than the
> approach I used if there are a /lot/ of items/properties!  There
> are ways to speed up my Items "property", though, such as using
> a binary search or "minimal perfect hash function" on Index when
> it's a string, so the Select Case is always done on a number,
> though it's usually pointless for less than a dozen or so items:

> private type luentry
>   key as string
>   num as long
> end type

> private function lookup(byref index as variant) as long
>   static lu() as luentry, initialized as boolean

>   if not initialized then
>     redim lu(0 to 3)
>     ' make sure the keys are in sorted order!
>     lu(0).key = "firstname"
>     lu(0).num = sffirstname
>     lu(1).key = "id"
>     lu(1).num = sfid
>     lu(2).key = "lastname"
>     lu(2).num = sflastname
>     lu(3).key = "registered"
>     lu(3).num = sfregistered
>     initialized = true
>   end if

>   if vartype(index) <> vbstring then
>     lookup = index
>     exit function
>   end if

>   ' Binary search is harder than it seems -- according to Knuth,
>   ' the first version was published in 1946, but the first /correct/
>   ' version wasn't published until 1962, /six{*filter*} years/ later!
>   dim l as long, u as long, m as long
>   l = lbound(lu)
>   u = ubound(lu)
>   do
>     m = (l + u) \ 2
>     select case strcomp(index, lu(m).key, vbtextcompare)
>     case is < 0
>       u = m - 1
>     case is > 0
>       l = m + 1
>     case else
>       lookup = lu(m).num
>       exit function
>     end select
>   loop while l <= u
>   lookup = -1
> end function

> public property get fields(byref index as variant) as variant
>   select case lookup(index)
>   case sfid
>     fields = id
>   case sflastname
>     fields = lastname
>   case sffirstname
>     fields = firstname
>   case sfregistered
>     fields = registered
>   case else
>     err.raise number:=5
>   end select
> end property

> --
> Joe Foster <mailto:jlfoster%40znet.com>     Got Thetans?

< http://www.*-*-*.com/ ;

- Show quoted text -

Quote:
> WARNING: I cannot be held responsible for the above        They're
coming  to
> because  my cats have  apparently  learned to type.        take me away,
ha ha!



Wed, 15 Sep 2004 14:35:15 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Implementing Class and Collection in VB

2. Implement a Class with a Collection Property

3. implement my own collection

4. Cannot implement Collection

5. How to implement collections in activex control?

6. VB Collections - Accessing the collection, not the item.

7. Why VBA.Collection and Not VB.Collection?

8. Collections, Collections and More Collections

9. why must I implement ICollection and IEnumerable whenever I want to implement IList

10. Problem implementing FTP in VB .Net with WinInet

11. Implementing Orders - Customers relations in vb.net

12. Implement interface in VB.Net

 

 
Powered by phpBB® Forum Software