- Code: Select all
// accumulate days by looping through the daysInMonths array
// and adding them as we go
for(i=1; i<=daysInMonths.length-1; i++){
// make the secondary incrementer equal to the current daysInMonths index
j = i;
// count backwards from j and add each index
// below current j in daysInMonths until j==0
// assign the accumulated value to the current i index of cumDays
while(j != 0){
cumDays[i] = cumDays[i] + daysInMonths[j];
j--
}
}
But now that I have run that loop and it has calculated what it needed to, I find that I can just hardcode the values in the array rather than having them calculated by the loop. They are based on the days of each month so they don't change (except feb, which is accounted for anyway). It means that the loop will run like 90 times each time the calendar is opened, whereas I can hardcode the values and skip the loop entirely.
My question is this: Is it worth keeping that loop? It took me a while to figure it out and I think it's nice, but it is entirely unnecessary. Does 936 runs through a loop have any noticeable effect? What about on mobile devices? And is this relevant to the larger scale question of robust programming? Should I hardcode any values that only need to be calculated once ever? Or should programming have more tricky shit like loops and arrays to calculate the crap every time? How many loops does it take before you notice a difference in speed?
I know you compsci aficionados will know all about this...




