Access 2003:第二个循环更快 [英] Access 2003: The Second Loop is the Faster

查看:55
本文介绍了Access 2003:第二个循环更快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

II首先在Access 2003中注意到了这种现象。简单地说它是:


第二个循环比第一个循环更快。

这就是if我们测试:

不是(a或b)对(不是)和(不是b)

在一千万次迭代循环中我们会发现我们

测试首先是两者中较慢的一个,无论是哪一个。


现在明确地说不是(a或b)应该比(不是)和(不是b)

更快,因为我们正在对三个进行两次操作。它是。

但是如果我们要测试(在同一模块中)不是(a或b)首先和(不是

a)和(不是b)秒我们结果可能相反。


简单的代码测试如下所示。

你可以复制我的结果吗?

你有吗?一个观点?

我在逻辑上出错吗?


私有声明函数GetTickCount Lib" kernel32" ()As Long

Private Const Iterations As Long = 10000000


Sub AlgebraicLogic1()

Dim t(2)As Long

Dim y As Long

Dim z As Long

Dim a As Boolean

Dim b As Boolean

Dim c As Boolean


t(0)= GetTickCount

y = 1到迭代次数

c = (不是)和(不是b)

下一个y

t(0)= GetTickCount - t(0)


t( 1)= GetTickCount

for z = 1 to Iterations

c = Not(a or b)

Next z

t(1)= GetTickCount - t(1)


Debug.Print 1,t(0),t(1),t(1)/ t(0)

End Sub


Sub AlgebraicLogic2()

Dim t(2)As Long

Dim y As Long

Dim z As Long

Dim a As Boolean

Dim b As Boolean

Dim c作为布尔值


t(0)= GetTickCount

对于y = 1到迭代次数

c =不是(a或b)

下一个y

t(0)= GetTickCount - t(0)

t(1)= GetTickCount

for z = 1 to Iterations

c =(不是)和(不是b)

下一个z

t(1)= GetTickCount - t(1)


Debug.Print 2,t(0),t(1),t(1)/ t(0)

结束子


1 541 301 0.55637707948244

2 460 331 0.719565217391304

1 521 301 0.577735124760077

2 471 320 0.679405520169851


1 521 310 0.595009596928983

2 461 320 0.6941431670282

1 521 310 0.595009596928983

2 491 331 0.674134419551935

解决方案

我的结果显示Not(A或B)每次都更快,无论是先运行

或秒。


1 484 453 0.935950413223141

2 453 484 1.06843267108168


1 484 453 0.935950413223141

2 453 469 1.03532008830022

1 484 454 0.93801652892562

2 453 484 1.06843267108168

-

Wayne Morgan

MS Access MVP

" Lyle Fairfield" < LY *********** @ aim.com>在消息中写道

news:11 ********************** @ p10g2000cwp.googlegr oups.com ...

II首先在Access 2003中注意到了这种现象。简单地说它是:

第二个循环比第一个循环更快。
那就是我们测试:
不是(a或b)反对(不是a)和(不是b)
在一千万次迭代的循环中我们会发现我们首先测试的条件是两者中较慢的一个,无论哪一个一个是。

现在明确地说不是(a或b)应该比(不是)和(不是b)更快,因为我们正在对三个进行两次操作。它是。
但是如果我们要测试(在同一模块中)不是(a或b)首先和(不是
a)和(非b)秒我们可能会得出相反的结论。 />
下面显示了一个简单的代码测试。
你可以复制我的结果吗?
你有意见吗?
我是否在逻辑上犯了错误?

私有声明函数GetTickCount Lib" kernel32" ()作为长期
私有Const迭代长= 10000000
子代数逻辑1()
Dim t(2)As Long
Dim y As Long
昏暗z很长
Dim a As Boolean
Dim b As Boolean
Dim c As Boolean

t(0)= GetTickCount
对于y = 1迭代
c =(不是)和(不是b)
下一个y
t(0)= GetTickCount - t(0)

t(1) = GetTickCount
对于z = 1到迭代次数
c =不(a或b)
下一个z
t(1)= GetTickCount - t(1)
< br。> Debug.Print 1,t(0),t(1),t(1)/ t(0)

子代数逻辑2()
Dim t(2)As Long
Dim y As Long
Dim z As Long
Dim a As Boolean
Dim b As Boolean
Dim c As Boolean

t(0)= GetTickCount
对于y = 1到Iterations
c = Not(a or b)
下一个y
t(0) = GetTickCount - t(0)

t(1)= GetTickCount
对于z = 1到迭代次数
c =(不是)和(不是b)
接下来z
t(1)= GetTickCount - t(1 )

Debug.Print 2,t(0),t(1),t(1)/ t(0)

End Sub
1 541 301 0.55637707948244
2 460 331 0.719565217391304
1 521 301 0.577735124760077
2 471 320 0.679405520169851
1 521 310 0.595009596928983
2 461 320 0.6941431670282
1 521 310 0.595009596928983
2 491 331 0.674134419551935



2006年3月15日05:14: 03 -0800,Lyle Fairfield

< ly *********** @ aim.com>写道:


有趣的现象。纯粹推测:可能是某些

变量可以保存在快速寄存器中(在CPU上),而其他变量则可以在缓慢的高速缓存中保存./ br / >

-Tom。

我首先在Access 2003中发现了这种现象。简单说明是:

第二个循环比第一个循环更快。
如果我们测试:
不是(a或b)反对(不是)和(不是b)
在千万循环中迭代我们会发现,我们首先测试的条件是两者中较慢的一个,无论是哪一个。

现在明确地说不是(a或b)。应该比(不是)和(不是b)更快,因为我们正在对三个进行两次操作。它是。
但是如果我们要测试(在同一模块中)不是(a或b)首先和(不是
a)和(非b)秒我们可能会得出相反的结论。 />
下面显示了一个简单的代码测试。
你可以复制我的结果吗?
你有意见吗?
我是否在逻辑上犯了错误?

私有声明函数GetTickCount Lib" kernel32" ()作为长期
私有Const迭代长= 10000000
子代数逻辑1()
Dim t(2)As Long
Dim y As Long
昏暗z很长
Dim a As Boolean
Dim b As Boolean
Dim c As Boolean

t(0)= GetTickCount
对于y = 1迭代
c =(不是)和(不是b)
下一个y
t(0)= GetTickCount - t(0)

t(1) = GetTickCount
对于z = 1到迭代次数
c =不(a或b)
下一个z
t(1)= GetTickCount - t(1)
< br / >Debug.Print 1,t(0),t(1),t(1)/ t(0)

子代数逻辑2()
Dim t(2)As Long
Dim y As Long
Dim z As Long
Dim a As Boolean
Dim b As Boolean
Dim c As Boolean

t(0)= GetTickCount
对于y = 1到Iterations
c = Not(a or b)
下一个y
t(0) = GetTickCount - t(0)

t(1)= GetTickCount
对于z = 1到迭代次数
c =(不是)和(不是b)
接下来z
t(1)= GetTickCount - t(1)

Debug.Print 2,t(0),t(1 ),t(1)/ t(0)
End Sub

1 541 301 0.55637707948244
2 460 331 0.719565217391304

1 521 301 0.577735124760077
2 471 320 0.679405520169851
1 521 310 0.595009596928983
2 461 320 0.6941431670282
1 521 310 0.595009596928983
2 491 331 0.674134419551935




2006年3月15日星期三格林尼治标准时间13:50:24,Wayne Morgan

<共*************************** @ hotmail.com>写道:

我的结果显示不是(A或B)每次都更快,无论是先运行还是第二次。

1 484 453 0.935950413223141
2 453 484 1.06843267108168
1 484 453 0.935950413223141
2 453 469 1.03532008830022
1 484 454 0.93801652892562
2 453 484 1.06843267108168



只是为了混淆问题,我的结果与你的结果相反。

即(不是)和(不是b)总是更快。


1 578 828 1.43252595155709

2 828 578 0.698067632850242

1 563 843 1.49733570159858

2 844 578 0.684834123222749

1 563 843 1.49733570159858

2 844 563 0.667061611374408

1 578 828 1.43252595155709

2 828 578 0.698067632850242


1 563 843 1.49733570159858

2 829 578 0.697225572979493

Wayne Gillespie

Gosford NSW Australia


II first noticed this phenomenon in Access 2003. Simply stated it is:

The second loop is faster than the first loop.
That is if we test:
Not (a Or b) against (Not a) And (Not b)
in a loop of ten million iterations we will find that the condition we
test first is the slower of the two, no matter which one that is.

Now clearly "Not (a Or b)" should be faster than "(Not a) And (Not b)"
as we are doing two operations against three. And it is.
But if we were to test (in the same module) Not (a Or b) first and (Not
a) And (Not b) second we might conclude the opposite.

A simple code test is shown below.
Can you duplicate my results?
Do you have an opinion?
Have I made an error in logic?

Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Const Iterations As Long = 10000000

Sub AlgebraicLogic1()
Dim t(2) As Long
Dim y As Long
Dim z As Long
Dim a As Boolean
Dim b As Boolean
Dim c As Boolean

t(0) = GetTickCount
For y = 1 To Iterations
c = (Not a) And (Not b)
Next y
t(0) = GetTickCount - t(0)

t(1) = GetTickCount
For z = 1 To Iterations
c = Not (a Or b)
Next z
t(1) = GetTickCount - t(1)

Debug.Print 1, t(0), t(1), t(1) / t(0)

End Sub

Sub AlgebraicLogic2()
Dim t(2) As Long
Dim y As Long
Dim z As Long
Dim a As Boolean
Dim b As Boolean
Dim c As Boolean

t(0) = GetTickCount
For y = 1 To Iterations
c = Not (a Or b)
Next y
t(0) = GetTickCount - t(0)

t(1) = GetTickCount
For z = 1 To Iterations
c = (Not a) And (Not b)
Next z
t(1) = GetTickCount - t(1)

Debug.Print 2, t(0), t(1), t(1) / t(0)

End Sub

1 541 301 0.55637707948244
2 460 331 0.719565217391304

1 521 301 0.577735124760077
2 471 320 0.679405520169851

1 521 310 0.595009596928983
2 461 320 0.6941431670282

1 521 310 0.595009596928983
2 491 331 0.674134419551935

解决方案

My results show Not(A Or B) to be faster each time, whether it is run first
or second.

1 484 453 0.935950413223141
2 453 484 1.06843267108168

1 484 453 0.935950413223141
2 453 469 1.03532008830022

1 484 454 0.93801652892562
2 453 484 1.06843267108168

--
Wayne Morgan
MS Access MVP
"Lyle Fairfield" <ly***********@aim.com> wrote in message
news:11**********************@p10g2000cwp.googlegr oups.com...

II first noticed this phenomenon in Access 2003. Simply stated it is:

The second loop is faster than the first loop.
That is if we test:
Not (a Or b) against (Not a) And (Not b)
in a loop of ten million iterations we will find that the condition we
test first is the slower of the two, no matter which one that is.

Now clearly "Not (a Or b)" should be faster than "(Not a) And (Not b)"
as we are doing two operations against three. And it is.
But if we were to test (in the same module) Not (a Or b) first and (Not
a) And (Not b) second we might conclude the opposite.

A simple code test is shown below.
Can you duplicate my results?
Do you have an opinion?
Have I made an error in logic?

Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Const Iterations As Long = 10000000

Sub AlgebraicLogic1()
Dim t(2) As Long
Dim y As Long
Dim z As Long
Dim a As Boolean
Dim b As Boolean
Dim c As Boolean

t(0) = GetTickCount
For y = 1 To Iterations
c = (Not a) And (Not b)
Next y
t(0) = GetTickCount - t(0)

t(1) = GetTickCount
For z = 1 To Iterations
c = Not (a Or b)
Next z
t(1) = GetTickCount - t(1)

Debug.Print 1, t(0), t(1), t(1) / t(0)

End Sub

Sub AlgebraicLogic2()
Dim t(2) As Long
Dim y As Long
Dim z As Long
Dim a As Boolean
Dim b As Boolean
Dim c As Boolean

t(0) = GetTickCount
For y = 1 To Iterations
c = Not (a Or b)
Next y
t(0) = GetTickCount - t(0)

t(1) = GetTickCount
For z = 1 To Iterations
c = (Not a) And (Not b)
Next z
t(1) = GetTickCount - t(1)

Debug.Print 2, t(0), t(1), t(1) / t(0)

End Sub

1 541 301 0.55637707948244
2 460 331 0.719565217391304

1 521 301 0.577735124760077
2 471 320 0.679405520169851

1 521 310 0.595009596928983
2 461 320 0.6941431670282

1 521 310 0.595009596928983
2 491 331 0.674134419551935



On 15 Mar 2006 05:14:03 -0800, "Lyle Fairfield"
<ly***********@aim.com> wrote:

Interesting phenomenon. Purely speculating: it could be that some
variables can be held in fast registers (on the CPU), while others go
in a bit slower cache memory.

-Tom.

II first noticed this phenomenon in Access 2003. Simply stated it is:

The second loop is faster than the first loop.
That is if we test:
Not (a Or b) against (Not a) And (Not b)
in a loop of ten million iterations we will find that the condition we
test first is the slower of the two, no matter which one that is.

Now clearly "Not (a Or b)" should be faster than "(Not a) And (Not b)"
as we are doing two operations against three. And it is.
But if we were to test (in the same module) Not (a Or b) first and (Not
a) And (Not b) second we might conclude the opposite.

A simple code test is shown below.
Can you duplicate my results?
Do you have an opinion?
Have I made an error in logic?

Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Const Iterations As Long = 10000000

Sub AlgebraicLogic1()
Dim t(2) As Long
Dim y As Long
Dim z As Long
Dim a As Boolean
Dim b As Boolean
Dim c As Boolean

t(0) = GetTickCount
For y = 1 To Iterations
c = (Not a) And (Not b)
Next y
t(0) = GetTickCount - t(0)

t(1) = GetTickCount
For z = 1 To Iterations
c = Not (a Or b)
Next z
t(1) = GetTickCount - t(1)

Debug.Print 1, t(0), t(1), t(1) / t(0)

End Sub

Sub AlgebraicLogic2()
Dim t(2) As Long
Dim y As Long
Dim z As Long
Dim a As Boolean
Dim b As Boolean
Dim c As Boolean

t(0) = GetTickCount
For y = 1 To Iterations
c = Not (a Or b)
Next y
t(0) = GetTickCount - t(0)

t(1) = GetTickCount
For z = 1 To Iterations
c = (Not a) And (Not b)
Next z
t(1) = GetTickCount - t(1)

Debug.Print 2, t(0), t(1), t(1) / t(0)

End Sub

1 541 301 0.55637707948244
2 460 331 0.719565217391304

1 521 301 0.577735124760077
2 471 320 0.679405520169851

1 521 310 0.595009596928983
2 461 320 0.6941431670282

1 521 310 0.595009596928983
2 491 331 0.674134419551935




On Wed, 15 Mar 2006 13:50:24 GMT, "Wayne Morgan"
<co***************************@hotmail.com> wrote:

My results show Not(A Or B) to be faster each time, whether it is run first
or second.

1 484 453 0.935950413223141
2 453 484 1.06843267108168

1 484 453 0.935950413223141
2 453 469 1.03532008830022

1 484 454 0.93801652892562
2 453 484 1.06843267108168


Just to confuse matters, my results are the opposite of yours.
ie (Not a) And (Not b) is always quicker.

1 578 828 1.43252595155709
2 828 578 0.698067632850242

1 563 843 1.49733570159858
2 844 578 0.684834123222749

1 563 843 1.49733570159858
2 844 563 0.667061611374408

1 578 828 1.43252595155709
2 828 578 0.698067632850242

1 563 843 1.49733570159858
2 829 578 0.697225572979493
Wayne Gillespie
Gosford NSW Australia


这篇关于Access 2003:第二个循环更快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆