Code efficiency problem

6 views
Skip to first unread message

yik...@clarkson.edu

unread,
Nov 12, 2017, 11:02:21 PM11/12/17
to AMPL Modeling Language
Hi,

Recently I observed a wired problem.
My code structure is like:
for {I=1...n}
{
if (C[i]==1)
{
A[i]=-1;
B[i]=-1;
}
}
The execution time of this piece of simple code is about 10 seconds. However, when I just change the order of two assignments like :

for {I=1...n}
{
if (C[i]==1)
{
B[i]=-1;
A[i]=-1;
}
}
the execution time is less than 1second and the result keeps the same.
I can not understand the reason. Thank you very much.




Robert Fourer

unread,
Nov 13, 2017, 9:28:30 PM11/13/17
to am...@googlegroups.com
We don't know of any obvious reason why one of your loops should run much slower than the other. Can you send us a complete example, in AMPL syntax, that we can use to reproduce this behavior on our computers?

Note that the following loop should run faster than either of your examples:

for {i in 1..n: C[i] == 1} {
let A[i] := -1;
let B[i] := -1;
}

Bob Fourer
am...@googlegroups.com

=======

From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of yik...@clarkson.edu
Sent: Sunday, November 12, 2017 9:23 PM
To: AMPL Modeling Language
Subject: [AMPL 15086] Code efficiency problem

Recently I observed a wired problem.
My code structure is like:
for {I=1...n}
{
if (C[i]==1)
{
A[i]=-1;
B[i]=-1;
}
}

The execution time of this piece of simple code is about 10 seconds. However, when I just change the order of two assignments like :

for {I=1...n}
{
if (C[i]==1)
{
B[i]=-1;
A[i]=-1;
}
}

the execution time is less than 1 second and the result keeps the same.
Reply all
Reply to author
Forward
0 new messages