4

why is an assign statement more efficient than not using assign?

co-workers say that:

assign
  a=3
  v=7
  w=8.

is more efficient than:

a=3.
v=7.
w=8.

why?

Bill
  • 1,237
  • 4
  • 21
  • 44

3 Answers3

7

You could always test it yourself and see... but, yes, it is slightly more efficient. Or it was the last time I tested it. The reason is that the compiler combines the statements and the resulting r-code is a bit smaller.

But efficiency is almost always a poor reason to do it. Saving a micro-second here and there pales next to avoiding disk IO or picking a more efficient algorithm. Good reasons:

  1. Back in the dark ages there was a limit of 63k of r-code per program. Combining statements with ASSIGN was a way to reduce the size of r-code and stay under that limit (ok, that might not be a "good" reason). One additional way this helps is that you could also often avoid a DO ... END pair and further reduce r-code size.

  2. When creating or updating a record the fields that are part of an index will be written back to the database as they are assigned (not at the end of the transaction) -- grouping all assignments into a single statement helps to avoid inconsistent dirty reads. Grouping the indexed fields into a single ASSIGN avoids writing the index entries multiple times. (This is probably the best reason to use ASSIGN.)

  3. Readability -- you can argue that grouping consecutive assignments more clearly shows your intent and is thus more readable. (I like this reason but not everyone agrees.)

Tom Bascom
  • 13,405
  • 2
  • 27
  • 33
  • 1
    Regarding 1): AFAIK an index is updated any time one of its fields is written. If you split assigning index fields into separate assign statements the index will get updated multiple times which is bad for performance. The dirty read is a valid consideration, but even more important you might violate a unique index if the intermediate state of the index fields already exists in the table. – idspispopd Mar 01 '19 at 08:18
1

basically doing:

a=3.
v=7.
w=8.

is the same as:

assign a=3.
assign v=7.
assign w=8.

which is 3 separate statements so a little more overhead. Therefore less efficient.

Progress does assign as one statement whether there is 1 or more variables being assigned. If you do not say Assign then it is assumed so you will do 3 statements instead of 1. There is a 20% - 40% reduction in R Code and a 15% - 20% performance improvement when using one assign statement. Why this is can only be speculated on as I can not find any source with information on why this is. For database fields and especially key/index fields it makes perfect sense. For variables I can only assume it has to do with how progress manages its buffers and copies data to and from buffers.

AquaAlex
  • 360
  • 7
  • 17
  • why is it more overhead? Doesn't the compiler have to calculate all 3 statements separately (even if only one 'assign' is used)? – Bill Jun 18 '13 at 12:32
  • Progress does assign as one statement whether there is 1 or more variables being assigned. If you do not say Assign then it is assumed so you will do 3 statements instead of 1. There is a 20% - 40% reduction in R Code and a 15% - 20% performance improvement when using one assign statement. Why this is can only be speculated on as I can not find any source with information on why this is. For database fields and especially key/index fields it makes perfect sense. For variables I can only assume it has to do with how progress manages its buffers and copies data to and from buffers. – AquaAlex Jun 19 '13 at 07:18
0

ASSIGN will combine multiple statements into one. If a, v and w are fields in your db, that means it will do something like INSERT INTO (a,v,w)...

rather than INSERT INTO (a)... INSERT INTO (v)

etc.

ilovebigmacs
  • 983
  • 16
  • 28
  • 1
    That makes sense for database assigns but what about loading variables in a program? It is more efficient in that case? – Bill Jun 14 '13 at 13:11
  • 1
    Yes, it is more efficient as well. It has to do with the way Progress handles accesses to memory. – ilovebigmacs Jun 14 '13 at 13:25