PC Review


Reply
Thread Tools Rate Thread

Array takes 6x longer to process VLOOKUP than worksheet table does

 
 
KGOldWolf
Guest
Posts: n/a
 
      4th Nov 2009
I am running comparative performance tests and have an odd result. I can
post the code but the outcome is a bit surprising.

I read 10k rows which splits records resulting in 22k rows. In that process
I do a total of 100k VLOOKUPs (10k * 10 cells). When I built the 22k row
output directly to the worksheet, it took 12 seconds. I inserted an array,
built the output there and then moved the entire array to the worksheet at
the end of the procedure. That reduced processing time to 10 seconds (20%
reduction in elapsed time).

I then moved the "lookup table" to an array and the processing time jumped
to 60 seconds!

The questions are: 1) does that make sense to you? and 2) do some functions
(like VLOOKUPs) become inefficient when used in arrays?

My guess is that I am doing something wrong like not setting the array as a
table (but I can't seem to figure out how to do that).

I can post the code but I don't want to tie up a lot of board space.... any
general ideas on this?


Thanks,
Ken
 
Reply With Quote
 
 
 
 
Peter T
Guest
Posts: n/a
 
      4th Nov 2009
I don't follow what you mean by "I then moved the "lookup table" to an
array".

Post your code together with clear instructions for someone who cannot see
what you what you have to recreate, without guessing what is intended!

Regards,
Peter T

"KGOldWolf" <(E-Mail Removed)> wrote in message
news:F612F61B-2C16-4E56-A870-(E-Mail Removed)...
>I am running comparative performance tests and have an odd result. I can
> post the code but the outcome is a bit surprising.
>
> I read 10k rows which splits records resulting in 22k rows. In that
> process
> I do a total of 100k VLOOKUPs (10k * 10 cells). When I built the 22k row
> output directly to the worksheet, it took 12 seconds. I inserted an
> array,
> built the output there and then moved the entire array to the worksheet at
> the end of the procedure. That reduced processing time to 10 seconds (20%
> reduction in elapsed time).
>
> I then moved the "lookup table" to an array and the processing time jumped
> to 60 seconds!
>
> The questions are: 1) does that make sense to you? and 2) do some
> functions
> (like VLOOKUPs) become inefficient when used in arrays?
>
> My guess is that I am doing something wrong like not setting the array as
> a
> table (but I can't seem to figure out how to do that).
>
> I can post the code but I don't want to tie up a lot of board space....
> any
> general ideas on this?
>
>
> Thanks,
> Ken



 
Reply With Quote
 
KG Old Wolf
Guest
Posts: n/a
 
      5th Nov 2009
Following are code snippets - the top example runs 6x faster than the bottom
(10 seconds versus 60). The only change was the way the VLOOKUPs are
performed.

By "moving the table to an array" I simply meant that the top approach uses
a table that is on the same worksheet as the rows that are used as input to
the procedure. The bottom approach is intended to compare the efficiency
gained by having that same information contained in an array (which I
expected to be faster).

I believe the problem in the bottom approach is caused because the array is
not "SET" in the way the table is in the top approach. The issue is that I
can't seem to find any information on how to set / reference the array data
any way other than what I have coded.

I hope this clarifies it for you.

Thanks for your advice,
Ken

+++++++++++++++++++++++++++++++++++++++++++++++++++

Array_Dates = Range("Table_Dates")
…..
......... Intervening code that remains the same in both examples
............
Print_Line_Array(Output_Row_Number, 8) =
Application.WorksheetFunction.VLookup(Lookup_Date, Array_Dates, 2, True)
' Days In Month
Print_Line_Array(Output_Row_Number, 9) =
Application.WorksheetFunction.VLookup(Lookup_Date, Array_Dates, 5, True)
' Month Sequence Number
Print_Line_Array(Output_Row_Number, 10) =
Application.WorksheetFunction.VLookup(Lookup_Date, Array_Dates, 3, True)
' First Day In Month

+++++++++++++++++++++++++++++++++++++++++++++

Set Table_Dates = Range(Cells(1, 51), Cells(61, 58))
ActiveWorkbook.Names.Add Name:="Table_Dates", RefersTo:=Range(Cells(2, 51),
Cells(61, 58))
…..
......... Intervening code that remains the same in both examples
............
Print_Line_Array(8) = Application.WorksheetFunction.VLookup(Lookup_Date,
Table_Dates, 2, True) ' Days In Month
Print_Line_Array(9) = Application.WorksheetFunction.VLookup(Lookup_Date,
Table_Dates, 5, True) ' Month Sequence Number
Print_Line_Array(10) = Application.WorksheetFunction.VLookup(Lookup_Date,
Table_Dates, 3, True) ' First Day In Month




 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      5th Nov 2009
OK I follow now. My test results for the array vs range were even worse than
yours, about 10x slower

Using a range it's about 2x faster to pass a reference to the range
eg
wfn.VLookup(Lookup_Date, Range(Cells(1, 51), Cells(61, 58)), c, True)
is about 2x slower than
Set Table_Dates = Range(Cells(1, 51), Cells(61, 58))
x = wfn.VLookup(Lookup_Date, Table_Dates, c, True)

If you really want to keep everything out of cells, following is only about
2x slower -

Array_Match = Range("Table_Dates").Columns(1)
m = wfn.Match(Lookup_Date, Array_Match, 0)
x = Array_Dates(m, c)

If your data is sorted you can probably achieve fastest results entirely
with VBA, ie no use of worksheet functions (search "binary chop").

fwiw you can do -
Dim wfn As WorksheetFunction
Set wfn = Application.WorksheetFunction

In passing, although notably, if the lookup is a date-value and the first
column of the lookup array also holds dates I got incorrect results using
the array method - watch out for that!

Regards,
Peter T



"KG Old Wolf" <(E-Mail Removed)> wrote in message
news:2488EC6D-383A-4EF7-BF24-(E-Mail Removed)...
> Following are code snippets - the top example runs 6x faster than the
> bottom
> (10 seconds versus 60). The only change was the way the VLOOKUPs are
> performed.
>
> By "moving the table to an array" I simply meant that the top approach
> uses
> a table that is on the same worksheet as the rows that are used as input
> to
> the procedure. The bottom approach is intended to compare the efficiency
> gained by having that same information contained in an array (which I
> expected to be faster).
>
> I believe the problem in the bottom approach is caused because the array
> is
> not "SET" in the way the table is in the top approach. The issue is that
> I
> can't seem to find any information on how to set / reference the array
> data
> any way other than what I have coded.
>
> I hope this clarifies it for you.
>
> Thanks for your advice,
> Ken
>
> +++++++++++++++++++++++++++++++++++++++++++++++++++
>
> Array_Dates = Range("Table_Dates")
> ...
> ........ Intervening code that remains the same in both examples
> ...........
> Print_Line_Array(Output_Row_Number, 8) =
> Application.WorksheetFunction.VLookup(Lookup_Date, Array_Dates, 2, True)
> ' Days In Month
> Print_Line_Array(Output_Row_Number, 9) =
> Application.WorksheetFunction.VLookup(Lookup_Date, Array_Dates, 5, True)
> ' Month Sequence Number
> Print_Line_Array(Output_Row_Number, 10) =
> Application.WorksheetFunction.VLookup(Lookup_Date, Array_Dates, 3, True)
> ' First Day In Month
>
> +++++++++++++++++++++++++++++++++++++++++++++
>
> Set Table_Dates = Range(Cells(1, 51), Cells(61, 58))
> ActiveWorkbook.Names.Add Name:="Table_Dates", RefersTo:=Range(Cells(2,
> 51),
> Cells(61, 58))
> ...
> ........ Intervening code that remains the same in both examples
> ...........
> Print_Line_Array(8) = Application.WorksheetFunction.VLookup(Lookup_Date,
> Table_Dates, 2, True) ' Days In Month
> Print_Line_Array(9) = Application.WorksheetFunction.VLookup(Lookup_Date,
> Table_Dates, 5, True) ' Month Sequence Number
> Print_Line_Array(10) = Application.WorksheetFunction.VLookup(Lookup_Date,
> Table_Dates, 3, True) ' First Day In Month
>
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatic Calculation in Code Takes Longer than Worksheet adambush4242@hotmail.com Microsoft Excel Programming 3 8th Jan 2009 06:18 PM
Vlookup Table Array ZootRot Microsoft Excel Discussion 5 27th Oct 2006 11:22 AM
Which is faster: VLOOKUP-worksheet or VB-array VLOOKUP? erikhs Microsoft Excel Programming 1 6th Aug 2006 06:18 PM
VLOOKUP - 3 Table Array tangomj Microsoft Excel Worksheet Functions 1 1st Aug 2006 05:43 PM
How to use a cell value as Table Array in VLOOKUP worksheet function willydlish Microsoft Excel Misc 2 16th Feb 2005 02:47 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:39 PM.