473,884 Members | 2,313 Online

efficiency of && vs. array

hi,

i'm working with data for a clinical trial. i write data edit-checks
using a program called DataFax, which has its own built-in programming
language which is loosely based on C. as such, the code example might
not have the exact same syntax as C, but the principle is the same.

i'm working on a edit check that will test a series of values, like
so:

if (a == 1 && b == 1 && c == 1 && d == 1)
then....

it occured to me that i could store a, b, c, and d in an array and use
an iterator perform the same function:

number i = 1;
group TEST a, b, c, d; /* create an array called TEST and store a,b,c,d
*/
if (condition) {
while (i <= 4) {
if (TEST[i] == 1) {...;}
i = i + 1;}
then....

my question is: which one of these methods is the best practice? is one
inherently more efficient than the other? am i making sense?

thanks,

chris

Feb 10 '06 #1
21 1579
christopher wrote:

hi,

i'm working with data for a clinical trial. i write data edit-checks
using a program called DataFax, which has its own built-in programming
language which is loosely based on C. as such, the code example might
not have the exact same syntax as C, but the principle is the same.

i'm working on a edit check that will test a series of values, like
so:

if (a == 1 && b == 1 && c == 1 && d == 1)
then....

it occured to me that i could store a, b, c, and d in an array and use
an iterator perform the same function:

number i = 1;
group TEST a, b, c, d; /* create an array called TEST and store a,b,c,d
*/
if (condition) {
while (i <= 4) {
if (TEST[i] == 1) {...;}
i = i + 1;}
then....

my question is: which one of these methods is the best practice?
is one
inherently more efficient than the other? am i making sense?

The first way is computationally less intensive.

--
pete
Feb 10 '06 #2
christopher wrote:
i'm working with data for a clinical trial. i write data edit-checks
using a program called DataFax, which has its own built-in programming
language which is loosely based on C. as such, the code example might
not have the exact same syntax as C, but the principle is the same.

i'm working on a edit check that will test a series of values, like
so:

if (a == 1 && b == 1 && c == 1 && d == 1)
then....

it occured to me that i could store a, b, c, and d in an array and use
an iterator perform the same function:

number i = 1;
group TEST a, b, c, d; /* create an array called TEST and store a,b,c,d
*/
if (condition) {
while (i <= 4) {
if (TEST[i] == 1) {...;}
i = i + 1;}
then....

my question is: which one of these methods is the best practice? is one
inherently more efficient than the other? am i making sense?

My recommendation is to program for maximum clarity. The difference in
efficiency is probably negligible (you know the three rules of
optimization, right?). What representation to use depends on where the
variables a, b, c and d come from. Are they related? How are they related?
August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
Feb 10 '06 #3
no - i don't know the three rules of optimization. what are they?

Feb 10 '06 #4
christopher wrote:

no - i don't know the three rules of optimization. what are they?

I can only remember the first two:

First rule of optimization: "Don't do it."
Second rule (for experts only): "Don't do it yet."

--
pete
Feb 10 '06 #5
christopher said:
no - i don't know the three rules of optimization. what are they?

I thought there were only two. They are:

Rule 1: Don't do it.

Rule 2 (for experts only): Don't do it /yet/.

My candidate for Rule 3 would be: If you must do this damn silly thing,
don't do it in this damn silly way.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Feb 10 '06 #6
Richard Heathfield wrote:
christopher said:
no - i don't know the three rules of optimization. what are they?

I thought there were only two. They are:

Rule 1: Don't do it.

Rule 2 (for experts only): Don't do it /yet/.

My candidate for Rule 3 would be: If you must do this damn silly thing,
don't do it in this damn silly way.

Mine would be: find out where to optimise by measurement, not guesswork.

--
Chris "do as I say, not as I have done ..." Dollin
Feb 10 '06 #7
In article <11************ *********@f14g2 000cwb.googlegr oups.com>,
"christophe r" <co*****@gmail. com> wrote:
hi,

i'm working with data for a clinical trial. i write data edit-checks
using a program called DataFax, which has its own built-in programming
language which is loosely based on C. as such, the code example might
not have the exact same syntax as C, but the principle is the same.

i'm working on a edit check that will test a series of values, like
so:

if (a == 1 && b == 1 && c == 1 && d == 1)
then....

it occured to me that i could store a, b, c, and d in an array and use
an iterator perform the same function:

number i = 1;
group TEST a, b, c, d; /* create an array called TEST and store a,b,c,d
*/
if (condition) {
while (i <= 4) {
if (TEST[i] == 1) {...;}
i = i + 1;}
then....

my question is: which one of these methods is the best practice? is one
inherently more efficient than the other? am i making sense?

It occured to me that you could write a, b, c, and d to a file and read
them back from that file everytime you need them. You could then use
some encryption to make the process of reading from the file more
secure.

You are not by any chance working on any IT project paid for by the
British government?
Feb 10 '06 #8
In article <11************ **********@o13g 2000cwo.googleg roups.com>,
"christophe r" <co*****@gmail. com> wrote:
no - i don't know the three rules of optimization. what are they?

Don't do it.
Don't do it yet.
Don't do it unless you measure the effect.
Feb 10 '06 #9

christopher wrote:
hi,

i'm working with data for a clinical trial. i write data edit-checks
using a program called DataFax, which has its own built-in programming
language which is loosely based on C. as such, the code example might
not have the exact same syntax as C, but the principle is the same.

i'm working on a edit check that will test a series of values, like
so:

if (a == 1 && b == 1 && c == 1 && d == 1)
then....

it occured to me that i could store a, b, c, and d in an array and use
an iterator perform the same function:

number i = 1;
group TEST a, b, c, d; /* create an array called TEST and store a,b,c,d
*/
if (condition) {
while (i <= 4) {
if (TEST[i] == 1) {...;}
i = i + 1;}
then....

my question is: which one of these methods is the best practice? is one
inherently more efficient than the other? am i making sense?

thanks,

chris

Assuming that the language you use is like C, and the "..." doesn't
suppress any important details of the variant, then:

1) The expresssion handling of C fix that if, by example, if a==1 is
false, the remainder expression is not evaluated. However, in the loop
version, it seems that all the comparations will be done before to
execute or not the "then" clause.

2) Use TEST[i] instead of a named variable goes, in most part of
compilers, to more CPU expensive evaluation:
* "a" could be translated with: 1) read memory at address of a.
memory"

3) And, of course, there are also the time to init "i", ...

Kind regards.

Feb 10 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.