Single loop containing multiple if-statements or multiple if-statements with loops inside? - loops

Is it recommended to create multiple if statements and nest loops inside of them or create one loop and nest multiple if statements inside of that? I am wondering in terms of memory, time, and other factors I may have overlooked.
Here is some pseudocode:
if a:
for i in range(500):
do x
if b:
for i in range(500):
do y
versus
for i in range(500):
if y:
do x
if z:
do p
In other words, when is the best time to do the if-statement. Inside a few nested loops or before any of the loops?
I am also wondering this because it may be more efficient to do one if statement at the beginning for a check then do the loops nested in this if statement. However, this might result in lots of repeated code.

The first way is the best for the performances, you do the test one time rather than at each turn
Of course I suppose there is no side effect making the two solutions different

In some situations, you won't be able to check the condition before the loop, but if you can this is the better solution because a condition is checked one time instead of every loop iteration.

It really depends what you are iterating through and what you need to check for!
If you need to check each iteration in both cases a and b you should go with option 2.
But if it's either a or b just go with option 1.

Related

How can I write parametric tests for a function using AUnit?

I have a function with several boundary conditions, I would like to check.
However, writing a procedure for every case, which has always exactly one nearly equal line in body, does not make that much sense for me.
Is there a simple way to have the values for the arguments and the result in an array of record and test all of them?
Right now I have a for loop in a single test procedure, which iterates over all variants, but this obviously bails out after the first fail.
However, what I want is that I see the results for every case, even if some fail in between.

eiffel: does the across structure move the cursor of current iterable structure?

I was wondering if the across structure uses an own cursor or a separated one? does it ensure that cursor hasn't moved and if so how can it be expressed for other examples?
ITERABLE uses so called external cursors, not internal ones merged with the underlying structure. As a result, iteration affects neither the structure nor any other cursor, created the same way. This is important to support nested or recursive iterations. For example, to find if there are duplicates, one can do the following:
across structure as i loop
across structure as j loop
if i.item = j.item then print ("Duplicates found.") end
end
end
Doing the same with internal cursors, like (note: the code is incorrect)
from structure.start until structure.after loop
x := structure.item
from structure.start until structure.after loop
if x = structure.item then print ("Duplicates found.") end
structure.forth
end
structure.forth
end
does not work, because the inner loop also changes the cursor of the outer loop.
The limitation of the cursors associated with ITERABLE is that the associated structure should not be changed during the whole course of iteration. This is not a theoretical limitation, but a practical one, to simplify implementation and to make it a bit more efficient.

In Verilog Procedural Interface, is it possible to scan through iteration loop several times?

We can use vpi_scan in the following way:
vpiHandle iter = vpi_iterate(property, handle);
if (iter)
while ( entry = vpi_scan(iter) )
/*code*/;
iter will be freed when vpi_scan() returns NULL.
But what if I need to scan through the loop several times? Is there a way to tell vpi_scan not to free the iterator, so that I could vpi_free_object when I'm done?
I think I can solve the problem using an additional container (to collect and keep all entries), but is it really needed?
EDIT:
1. I would not like to call vpi_iterate more than once, since it can be expensive.
2. Suppose I go with an additional container solution. Is there a way to find out the number of entries in the vpi_scan loop without actual scanning through the loop? This could make implementation simpler.
ut what if I need to scan through the loop several times? vpi_iterate returns an initialized pointer to the iterator. Every vpi_scan removes an element from the list and frees it. If vpi_scan did not run till the end, you'd better use vpi_free_object to clean the rest of the iterator list. If you need to rescan the same object again, you can call vpi_iterate again and it will return a new iterator object which you can re-scan.
s there a way to tell vpi_scan not to free the iterator, so that I could vpi_free_object when I'm done? No, (1) is the only mechanism which can be used to access and free iterator elements. There is no other exist in the standard.
I think I can solve the problem using an additional container (to collect and keep all entries), but is it really needed? -- this is a good idea if you want to re-scan your data structs. It could be much better from the performance point of view. Verilog simulator usually has a separately generated vpi database which it needs to consult, probably multiple times to provide you with vpi access information.
Is there a way to find out the number of entries in the vpi_scan loop without actual scanning through the loop? not really. there are no defined way to query this information from the iterator. There might be a work-around with using vpi_handle(vpiUse, iterator) but it depends on the underlying data and the type of iteration. It is much easier to use linked lists created by the first scanning loop.
you can get all additional information for LRM or a verilog pli handbook.

Create vector of symbolic variables by name through iteration

I'm trying to create a growing vector inside a for-loop, i.e. the vector is initialized with one element (lets call it q_1) and in the next iteration of the loop the vector is expanded by a second element (lets call it q_2) and so on. Every element is a symbolic element depending on another independent element (in my case that's the time t).
The overall idea is to create a growing second order ODE that I want to solve and plot in every loop.
The first step I already implemented, is the for-loop but I struggle with creating a new symbolic variable inside the loop. My first idea was to create the variable by name so I can use the iteration index i of the loop and create a new unique string every loop, but I don't know how to accomplish this with Maxima.
There are various ways.
use subscripted symbolic variables, e.g., u[1], u[2], u[3], ....
use gensyms (generated symbol names which are guaranteed to be different from any other existing symbol. E.g. gensym() returns something like g4282, then next time gensym() returns g4283, etc.
construct a symbol via concat, e.g. concat('foo, 3) returns foo3. THIS IS THE SOLUTION OF LAST RESORT. TRY ANYTHING ELSE BEFORE RESORTING TO CONCAT. It is too easy to make a mess that way.
If you show the code you have so far, maybe I can give more specific advice.
Building upon the accepted answer, here is an example (using the concat method) of a for loop creating a matrix M with "indexed" symbols e_ij as elements:
M: zeromatrix(6,6)$
for r: 1 thru 6 do (
for c: 1 thru 6 do (
M[r][c]: concat('e_,r,c)
)
)$

Searching in only one subarray of 3d array in Processing

I have a 3D array of int values and I want to search just through one of the subarrays for a specific value. While I could for-loop my way through every possible combination of the below code.
array[numberIwant][1-255[1-255];
That seems like overkill. I've come across the foreach type of for and thought that might be the answer to my quest but either it's not or I don't understand it well enough to get it to work. Could anyone suggest the way this should be done?
The foreach and the for loop will have almost identical processing time for an array of that size.
Although it might seem like overkill it is not, you will need to do a triple nested for loop then have an if statement seeing if the number you want was found.

Resources