Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 747441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:12:19+00:00 2026-05-14T14:12:19+00:00

Consider this code: function Foo(ds as OtherDLL.BaseObj) dim lngRowIndex as long dim lngColIndex as

  • 0

Consider this code:

function Foo(ds as OtherDLL.BaseObj)
    dim lngRowIndex as long 
    dim lngColIndex as long

    for lngRowIndex = 1 to ubound(ds.Data, 2)
        for lngColIndex = 1 to ds.Columns.Count
            Debug.Print ds.Data(lngRowIndex, lngColIndex)
        next
    next
end function

OK, a little context. Parameter ds is of type OtherDLL.BaseObj which is defined in a referenced ActiveX DLL. ds.Data is a variant 2-dimensional array (one dimension carries the data, the other one carries the column index. ds.Columns is a Collection of columns in ‘ds.Data`.

Assuming there are at least 400 rows of data and 25 columns, this code takes about 15 seconds to run on my machine. Kind of unbelievable.

However if I copy the variant array to a local variable, so:

function Foo(ds as OtherDLL.BaseObj)
    dim lngRowIndex as long 
    dim lngColIndex as long
    dim v as variant

    v = ds.Data
    for lngRowIndex = 1 to ubound(v, 2)
        for lngColIndex = 1 to ds.Columns.Count
            Debug.Print v(lngRowIndex, lngColIndex)
        next
    next
end function

the entire thing processes in barely any noticeable time (basically close to 0).

Why?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:12:19+00:00Added an answer on May 14, 2026 at 2:12 pm

    Chances are that ds.Data is creating/copying the entire array each time it is accessed (e.g. by fetching it from a database or something), and this is taking a significant amount of time.

    By accessing it inside the loop you are then doing this copy operation hundreds of times. In the second case, you copy it once into a local variable outside the loop, and then quickly re-use that data hundreds of times without having to actually copy it again.

    This is a common and very important optimisation technique: move any unnecessary work out of the loop so it’s executed as few times as possible. It’s just that in your example, it’s not “obvious” that it’s doing lots of extra work when you access Data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this code: function Foo() { } Foo.prototype.alert = function() { alert(this); } (new
Consider the following code: function Dog() { this.foo = function() {}; this.walk = function()
Consider this javascript code: var bar = function () { alert(A); } var foo
Consider this (rather pointless) javascript code: function make_closure() { var x = 123, y
Consider this code: var img = new Image(); img.onload = function() { console.log(this.width); };
Consider the following base code: (function($) { $.fn.myPlugin = function(settings) { return this.each(function() {
Consider this example Javascript code: a = new Array(); a['a1']='foo'; a['a2']='bar'; b = new
Consider foo.sh: #!/bin/bash function foo() { source another.sh echo This shouldn't be executed. Return
Consider the following JavaScript code which has a recursive boom function function foo() {
Consider the Lua code below: local util = {} function util:foo(p) print (p or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.