efficiency of && vs. array

C

christopher

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 == 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
 
P

pete

christopher said:
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 == 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.
 
A

August Karlstrom

christopher said:
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 == 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
 
R

Richard Heathfield

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.
 
C

Chris Dollin

Richard said:
christopher said:


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.
 
C

Christian Bau

"christopher said:
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 == 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?
 
C

Christian Bau

"christopher said:
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.
 
T

tmp123

christopher said:
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 == 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 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.
* "TEST" could be evaluated with: 1) add i to TEST address; 2) read
memory"

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

Kind regards.
 
R

Richard Heathfield

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

That's another way of saying the same thing. :)

Actually, please forgive me for saying so, Chris, but I think mine is more
generally useful.
 
L

Laurent Deniau

christopher said:
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 == 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?


Assuming that you have considered the rules from previous post on
premature optimisation and decided that they do not apply to your case,
and assuming that a, b, c, d are of integer type then:

if (1 & a & b & c & d) {

}

will be probably slightly faster since it generates only one test which
should be better on processors with branch prediction.

a+, ld.
 
C

Chris Dollin

Richard said:
Chris Dollin said:


That's another way of saying the same thing. :)

Well ... certainly mine is an instance of yours, but yours applies to
more situations than optimisation [1] ...
Actually, please forgive me for saying so, Chris, but I think mine is more
generally useful.
[2]


.... but gives less specific guidance; which works best is likely
target-dependent.

How about a revised your-3:

3. Don't be stupid about it.

[1] EG swimming from London to Paris by way of Seattle.

[2] Forgive? Moi? My grudges are held for eternity! No second of my time
will be wasted in avoiding plotting your downfall!! My arcane influence
is unavoidable!!!

<fx:knocking/>

Why, there's Alec Guinness and his friends, come for a pint!!!!
 
R

Richard Heathfield

Laurent Deniau said:
Assuming that you have considered the rules from previous post on
premature optimisation and decided that they do not apply to your case,
and assuming that a, b, c, d are of integer type then:

if (1 & a & b & c & d) {

}

will be probably slightly faster since it generates only one test which
should be better on processors with branch prediction.

But it doesn't test a, b, c and d against 1. Rather, it checks whether they
have the least significant bit set. Not the same thing.
 
R

Richard Heathfield

Chris Dollin said:
How about a revised your-3:

3. Don't be stupid about it.

That's even more widely applicable, as it applies to absolutely everything.
 
L

Laurent Deniau

Richard said:
Laurent Deniau said:




But it doesn't test a, b, c and d against 1. Rather, it checks whether they
have the least significant bit set. Not the same thing.

Are you sure?

a+, ld.
 
L

Laurent Deniau

Richard said:
Laurent Deniau said:




But it doesn't test a, b, c and d against 1. Rather, it checks whether they
have the least significant bit set. Not the same thing.

Sorry you are right (I had bool in mind):

if ((1 & a & b & c & d) == (1 | a | b | c | d)) {
}

should be better.

a+, ld.
 
C

Chris Dollin

Richard said:
Chris Dollin said:


That's even more widely applicable, as it applies to absolutely
everything.

In context, `it` is bound to `optimisation` ...

I'm stopping now. Buy you a pint sometime?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top