+ Reply to Thread
Results 1 to 4 of 4

Faster For-Next Loop?

Hybrid View

  1. #1
    dgp@dodgeit.com
    Guest

    Faster For-Next Loop?

    In the function below I have a For-Next loop that loops though a range
    of cells. Would it be any faster to read the range into an array and
    loop through the array?

    Function InvDistanceWtd(x, y, xdata as Range, ydata as Range, zdata as
    Range, power)
    N = Application.Count(xdata)
    For i = 1 To N
    D = Sqr((x - xdata(i, 1)) ^ 2 + (y - ydata(i, 1)) ^ 2)
    SumZoDn = SumZoDn + data(i, 1) / D ^ Power
    SumIDn = SumIDn + 1 / D ^ Power
    Next i
    InvDistanceWtd = SumZoDn / SumIDn
    End Function


  2. #2
    Niek Otten
    Guest

    Re: Faster For-Next Loop?

    Depending on the size of your range, but in general: a LOT faster!
    The arrays in my functions are generally 120 elements (actuarial tables);
    difference in the order of 10 times faster.

    <dgp@dodgeit.com> wrote in message
    news:1105123565.180885.268380@z14g2000cwz.googlegroups.com...
    > In the function below I have a For-Next loop that loops though a range
    > of cells. Would it be any faster to read the range into an array and
    > loop through the array?
    >
    > Function InvDistanceWtd(x, y, xdata as Range, ydata as Range, zdata as
    > Range, power)
    > N = Application.Count(xdata)
    > For i = 1 To N
    > D = Sqr((x - xdata(i, 1)) ^ 2 + (y - ydata(i, 1)) ^ 2)
    > SumZoDn = SumZoDn + data(i, 1) / D ^ Power
    > SumIDn = SumIDn + 1 / D ^ Power
    > Next i
    > InvDistanceWtd = SumZoDn / SumIDn
    > End Function
    >




  3. #3
    Dana DeLouis
    Guest

    Re: Faster For-Next Loop?

    Hello. You pass "zdata" to your function, but I don't see it used. Is
    there a typo?

    --
    Dana DeLouis
    Win XP & Office 2003


    <dgp@dodgeit.com> wrote in message
    news:1105123565.180885.268380@z14g2000cwz.googlegroups.com...
    > In the function below I have a For-Next loop that loops though a range
    > of cells. Would it be any faster to read the range into an array and
    > loop through the array?
    >
    > Function InvDistanceWtd(x, y, xdata as Range, ydata as Range, zdata as
    > Range, power)
    > N = Application.Count(xdata)
    > For i = 1 To N
    > D = Sqr((x - xdata(i, 1)) ^ 2 + (y - ydata(i, 1)) ^ 2)
    > SumZoDn = SumZoDn + data(i, 1) / D ^ Power
    > SumIDn = SumIDn + 1 / D ^ Power
    > Next i
    > InvDistanceWtd = SumZoDn / SumIDn
    > End Function
    >




  4. #4
    dgp@dodgeit.com
    Guest

    Re: Faster For-Next Loop?

    Yes, I was in the process of renaming the range variable data (used in
    line 5) to zdata.

    Dana DeLouis wrote:
    > Hello. You pass "zdata" to your function, but I don't see it used.

    Is
    > there a typo?
    >
    > --
    > Dana DeLouis
    > Win XP & Office 2003
    >
    >
    > <dgp@dodgeit.com> wrote in message
    > news:1105123565.180885.268380@z14g2000cwz.googlegroups.com...
    > > In the function below I have a For-Next loop that loops though a

    range
    > > of cells. Would it be any faster to read the range into an array

    and
    > > loop through the array?
    > >
    > > Function InvDistanceWtd(x, y, xdata as Range, ydata as Range, zdata

    as
    > > Range, power)
    > > N = Application.Count(xdata)
    > > For i = 1 To N
    > > D = Sqr((x - xdata(i, 1)) ^ 2 + (y - ydata(i, 1)) ^ 2)
    > > SumZoDn = SumZoDn + data(i, 1) / D ^ Power
    > > SumIDn = SumIDn + 1 / D ^ Power
    > > Next i
    > > InvDistanceWtd = SumZoDn / SumIDn
    > > End Function
    > >



+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1