6 ms·
Looks quite elegant :-) You should probably use else-if instead of ifs though, otherwise it is (immediately) complicated to e.g. count the number of instruction
by oleks 11y ago
Looks quite elegant :-) You should probably use else-if instead of ifs though, otherwise it is (immediately) complicated to e.g. count the number of instructions you execute.
- rootlocus 11y agoI don't get it. How does using else-if make it less complicated to count the number of instructions?
- hartator 11y agoBecause it will force only to execute one of the conditional branch. https://github.com/wkoszek/cpu60/blob/master/cpu60.c https://github.com/wkoszek/cpu60/blob/master/cpu60.c might execute several. (It may be on purpose though.)
- mtdewcmu 11y agoIt won't execute more than one, because only one can match. The other sscanfs will all fail. I agree it would make sense to stop trying patterns once one has matched, though. Personally, my inclination would be to rewrite the loop to tighten it up. But maybe the point was to show how quickly and dirtily one can do this... it definitely looks quick and dirty.
- wkoszek 11y agoQuick, dirty and primitive was my primary motivation for this exercise :-)
- rootlocus 11y agoHow would you rewrite it to "tighten it up"?
- mtdewcmu 11y agoI would make that series of if-statements more obviously deterministic, for starters. One way of accomplishing that would be a bunch of nested if-else blocks. However, that would be ugly. I'd prefer something like continue statements -- which would necessitate redesigning the loop to avoid bypassing the code at the bottom. Either way, the way this currently gives the appearance of being possibly nondeterministic makes me uncomfortable... and besides, executing those extra conditionals wastes CPU cycles.
- SeldomSoup 11y agoWith the current code, you would need a counter increment for every if statement.
- rootlocus 11y agoAnd with the else-if code you would need a counter increment for every else-if statement
- SeldomSoup 11y agoNo, you'd just need one increment after all of them. An increment in every else-if block is redundant because only one condition can be executed per loop.