Say that "haveHistory" is a global flag which is initially false and it is written on inside the if( ! haveHistory ) statement. Then, that if statement will be satisfied only the first time. Now, does consume more energy a comparison and a jump or a memory cell write?
2009/5/30 Javier M Mora Merchan <[email protected]> > Raffaele Gravina escribió: > > Hi all, > > > > I'd like to bring to your attention the following two equivalent snippet > > of code: > > > > *************** A ******************** > > event void Read.readDone(error_t result, val_t val) { > > > > curr = val; > > > > if (haveHistory && (pre - curr) > 0)) { > > // do something > > } > > > > pre = curr; > > > > haveHistory = TRUE; > > } > > ***************************************** > > > > *************** B ******************** > > event void Read.readDone(error_t result, val_t val) { > > > > curr = val; > > > > if (haveHistory && (pre - curr) > 0)) { > > // do something > > } > > > > pre = curr; > > > > if( ! haveHistory ) > > haveHistory = TRUE; > > } > > ***************************************** > > > > > > Given that A and B are semantically the same and hence the "if ( > > ! haveHistory )" in B could be removed with no effect to the correctness, > > let's suppose the readDone event fires quite often; my question is: > > > > Is A more time and energy efficient than B? > > > > I'm pretty sure the "if ( ! haveHistory )" statement takes more CPU > > cycles than the simple "haveHistory = TRUE" assignement, so A should be > > definitely quicker than B; > > however, what about the energy consumed?!? > > > > I don't know :-) these are my thoughts: > > * Compilers had very good optimizers. It's possible both snippets > compile to the same assembler code. If you want to do so fine > calculations you have to compare generated asm code not original nesc code. > > * In A you have a assignation only. In B, you have a comparison and a > jump (in false case) or a comparison and assignation (in true case). > What are the probability of every case? > > > > _______________________________________________ > Tinyos-help mailing list > [email protected] > https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help > -- Raffaele Gravina Research Engineer Wireless Sensor Networks Lab Berkeley 2000 Hearst Ave, suite 304 Berkeley, CA 94709 +1 510 666 0174 ext. 101
_______________________________________________ Tinyos-help mailing list [email protected] https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help
