Optimizing the wrong way



  • Found this little snippet of code on some software I'm working on that runs in an embedded device which does great justice to highlight the quality of the code. Resources are limited (3MB storage, 1.5MB memory, and runs at around 12MHz).
    The only optimization the original coder was to name all variables as short as possible. The python is compiled into optimized bytecode.

     

    	if(v==0):
    		v=0
    	else:
    		v=1
    	return v
    


  • @spxza said:

    Resources are limited (3MB storage, 1.5MB memory, and runs at around 12MHz).

    Luxury ! On my current project, the main processor has 256kB flash, 64kB RAM, but at least a decent clock rate. The auxiliary processors have 32kB flash, 1536 bytes RAM and a 4MHz oscillator - divided by 4 for a 1MHz basic instruction cycle.



  • @SenTree said:

    @spxza said:

    Resources are limited (3MB storage, 1.5MB memory, and runs at around 12MHz).

    Luxury ! On my current project, the main processor has 256kB flash, 64kB RAM, but at least a decent clock rate. The auxiliary processors have 32kB flash, 1536 bytes RAM and a 4MHz oscillator - divided by 4 for a 1MHz basic instruction cycle.

    Well, if you're building a toaster, that's probably a bit overengineered, but if you're flying a space shuttle, it's probably not enough.

    What kind of project is it, anyway? 



  • @DaveK said:

    Well, if you're building a toaster, that's probably a bit overengineered, but if you're flying a space shuttle, it's probably not enough.

    Actually that sounds about right. From http://en.wikipedia.org/wiki/Space_Shuttle#Flight_systems

    The IBM AP-101 computers originally had about 424 kilobytes of magnetic core memory each. The CPU could process about 400,000 instructions per second. They have no hard disk drive, and load software from magnetic tape cartridges.

    In 1990, the original computers were replaced with an upgraded model AP-101S, which has about 2.5 times the memory capacity (about 1 megabyte) and three times the processor speed (about 1.2 million instructions per second). The memory was changed from magnetic core to semiconductor with battery backup.



  • @DaveK said:

    @SenTree said:

    @spxza said:

    Resources are limited (3MB storage, 1.5MB memory, and runs at around 12MHz).

    Luxury ! On my current project, the main processor has 256kB flash, 64kB RAM, but at least a decent clock rate. The auxiliary processors have 32kB flash, 1536 bytes RAM and a 4MHz oscillator - divided by 4 for a 1MHz basic instruction cycle.

    Well, if you're building a toaster, that's probably a bit overengineered, but if you're flying a space shuttle, it's probably not enough.

    What kind of project is it, anyway? 

    Not too far above a toaster - industrial sensing/monitoring. I can't be more specific because it's a fairly small niche and if you knew that, you could guess the company. That would blow my cover since I'm the only full-time SWE here !



  • Why the crap is Python running on such a low-end system? That's what Assembly is for.

    @SenTree said:

    @spxza said:
    Resources are limited (3MB storage, 1.5MB memory, and runs at around 12MHz).

    Luxury ! On my current project, the main processor has 256kB flash, 64kB RAM, but at least a decent clock rate. The auxiliary processors have 32kB flash, 1536 bytes RAM and a 4MHz oscillator - divided by 4 for a 1MHz basic instruction cycle.

    Try the NES. ~1.5mhz 6502, 2K RAM, 32K ROM. Cartridges could employ bankswitching and have additional (slower) RAM on them, but to a very limited extent. Of course programmers were encouraged to refrain from that if at all possible to keep costs down. (And the 6502 is weeeeaaaaak!)



  • @lolwtf said:

    @SenTree said:
    @spxza said:
    Resources are limited (3MB storage, 1.5MB memory, and runs at around 12MHz).

    Luxury ! On my current project, the main processor has 256kB flash, 64kB RAM, but at least a decent clock rate. The auxiliary processors have 32kB flash, 1536 bytes RAM and a 4MHz oscillator - divided by 4 for a 1MHz basic instruction cycle.
    Try the NES. ~1.5mhz 6502, 2K RAM, 32K ROM. Cartridges could employ bankswitching and have additional (slower) RAM on them, but to a very limited extent. Of course programmers were encouraged to refrain from that if at all possible to keep costs down. (And the 6502 is weeeeaaaaak!)

    Those were the days ! I won't describe my very first computer (the 'Four Yorkshiremen' meme is currently playing in another thread). Would you believe we still have a product with bank-switched ROM, and it was only designed ten years ago.



  • @lolwtf said:

    Try the NES. ~1.5mhz 6502, 2K RAM, 32K ROM. Cartridges could employ bankswitching and have additional (slower) RAM on them, but to a very limited extent. Of course programmers were encouraged to refrain from that if at all possible to keep costs down. (And the 6502 is weeeeaaaaak!)

    Minor nitpick - the CPU is 1.79MHz (for NTSC; for PAL it's only 1.66MHz), extra RAM on cartridges isn't slower (it's the same speed as the cartridge ROM and the console's builtin RAM), and you'd be surprised just how powerful some cartridge expansion hardware is (Konami VRC7 had 6 channel FM synth audio, MMC5 was extremely versatile). Also, the 6502 isn't weak - clock for clock, it's more powerful than the Z80.



  • @SenTree said:

    @spxza said:

    Resources are limited (3MB storage, 1.5MB memory, and runs at around 12MHz).

    Luxury ! On my current project, the main processor has 256kB flash, 64kB RAM, but at least a decent clock rate. The auxiliary processors have 32kB flash, 1536 bytes RAM and a 4MHz oscillator - divided by 4 for a 1MHz basic instruction cycle.

    Oscillator! You were lucky to have an oscillator! We had to generate a clock signal by toggling a switch on and off as fast as we could!



  • I have seen a computer with a crank handle.  It was used for stepping through the program one operation at a time.  The crank had a hexagonal or octagonal cam on it that switched a switch 6 or 8 times per crank.  Each bump on the cam was a clock signal.

    Of course it was built back when ENIAC was hot stuff.



  • @Quietust said:

    Also, the 6502 isn't weak - clock for clock, it's more powerful than the Z80.
     

    I agree with whatever Quietust just posted above! 


  • Garbage Person

    @Qwerty said:

    I have seen a computer with a crank handle.  It was used for stepping through the program one operation at a time.  The crank had a hexagonal or octagonal cam on it that switched a switch 6 or 8 times per crank.  Each bump on the cam was a clock signal.

    Of course it was built back when ENIAC was hot stuff.

    This is the COOLEST thing I've ever heard. I shall begin work on the USB Debugging Crank immediately!


  • @Quietust said:

    Also, the 6502 isn't weak - clock for clock, it's more powerful than the Z80.
     

    But you don't compare clock for clock. The 6502 went up to (IIRC) 2MHz, where the Z80 went up to 8MHz. Now, most if not all instructions on the Z80 were multi-cycle ones where more were single cycle on the 6502. The 6502 had quite a few limitations (eg 8 bit index register, few registers etc).

    For a PROPER CPU of that era, you want the 6809 - far superior to either the 6502 or Z80 - built in multiply instruction, 16 bit arithmetic, 4 x 16 bit index registers (including 2 stack pointers which can also be used for general purpose ops). Oh, the memories!

     



  • uh.......wtf? Talk about a serious side track.

     Anyways, to continue in the same kinda vein: Can anyone point me in the direction of a nice set of 16F87 programming resources?



  •  Google's first hit (with the educated guess that it's a pic-processor):

    http://ww1.microchip.com/downloads/en/devicedoc/30487c.pdf

     For other programming instructions, just look up something generic about pic processing.

     @senTree: what's wrong with the axillary pic-processors? :P



  • @spxza said:

    uh.......wtf? Talk about a serious side track.

     Anyways, to continue in the same kinda vein: Can anyone point me in the direction of a nice set of 16F87 programming resources?

    As the original derailer I feel a bit guilty - sorry for that, it wasn't my intention !

    I haven't used PIC16s for a while, but there's obviously Microchip's own site to start with, plenty of forums and application notes there:

    http://www.microchip.com/forums/Default.aspx?

    You could also try the Hi-Tech site: http://forum.htsoft.com/all/ubbthreads.php/Cat/0/C/6

    or CCS: http://www.ccsinfo.com/forum/



  • @mbvlist said:

     @senTree: what's wrong with the axillary pic-processors? :P

    They're the (arm)pits ? (I'm guessing that was an intentional typo).
    Actually, they're fine for the application; wouldn't have been my first choice but it wasn't my decision.



  • @spxza said:

    Found this little snippet of code on some software I'm working on that runs in an embedded device which does great justice to highlight the quality of the code. Resources are limited (3MB storage, 1.5MB memory, and runs at around 12MHz).
    The only optimization the original coder was to name all variables as short as possible. The python is compiled into optimized bytecode.

     

    	if(v==0):
    		v=0
    	else:
    		v=1
    	return v
    

    This is, of course, just the bog-standard "x = !!x" formulation used to normalize a bool into the 0-1 range.  With a superfluous assignment in the zero case.



  • @autorelease said:

    @SenTree said:
    @spxza said:

    Resources are limited (3MB storage, 1.5MB memory, and runs at around 12MHz).

    Luxury ! On my current project, the main processor has 256kB flash, 64kB RAM, but at least a decent clock rate. The auxiliary processors have 32kB flash, 1536 bytes RAM and a 4MHz oscillator - divided by 4 for a 1MHz basic instruction cycle.

    Oscillator! You were lucky to have an oscillator! We had to generate a clock signal by toggling a switch on and off as fast as we could!

     

    We used to dream of having a switch!  In my day we had to complete the circuit by touching two bare wires to our tongues!



  • @dwilliss said:

    @autorelease said:

    @SenTree said:
    @spxza said:

    Resources are limited (3MB storage, 1.5MB memory, and runs at around 12MHz).

    Luxury ! On my current project, the main processor has 256kB flash, 64kB RAM, but at least a decent clock rate. The auxiliary processors have 32kB flash, 1536 bytes RAM and a 4MHz oscillator - divided by 4 for a 1MHz basic instruction cycle.

    Oscillator! You were lucky to have an oscillator! We had to generate a clock signal by toggling a switch on and off as fast as we could!

     

    We used to dream of having a switch!  In my day we had to complete the circuit by touching two bare wires to our tongues!

    Wires?  Wires?  You had the sheer brazen decadent luxury of wires?  Pah!  When I wer't lad, we 'ad to carry electrons from't anode to't cathode one-by-one using a very very small pair of tweezers, and if we sneezed and dropped one, Father would beat us to death with a rusty porcupine.  And we called ourselves lucky! 



  • @SenTree said:

    @spxza said:

    uh.......wtf? Talk about a serious side track.

     Anyways, to continue in the same kinda vein: Can anyone point me in the direction of a nice set of 16F87 programming resources?

    As the original derailer I feel a bit guilty - sorry for that, it wasn't my intention !

    I haven't used PIC16s for a while, but there's obviously Microchip's own site to start with, plenty of forums and application notes there:

    http://www.microchip.com/forums/Default.aspx?

    You could also try the Hi-Tech site: http://forum.htsoft.com/all/ubbthreads.php/Cat/0/C/6

    or CCS: http://www.ccsinfo.com/forum/

     

     

    Microchip now own Hitech.

     

    So now not only to they make crap micros they also make poor compilers......



  • @DaveK said:

    Father would beat us to death with a rusty porcupine
     

    He beat you with [url=http://en.wikipedia.org/wiki/Porcupine]an animal[/url]? A rusty one furthermore?

    Oh wait, this was about imaginary 1-upping the previous post wasn't it? okay, okay, I'll go with you

    @DaveK said:

    @dwilliss said:

    @autorelease said:

    @SenTree said:
    @spxza said:

    Resources are limited (3MB storage, 1.5MB memory, and runs at around 12MHz).

    Luxury ! On my current project, the main processor has 256kB flash, 64kB RAM, but at least a decent clock rate. The auxiliary processors have 32kB flash, 1536 bytes RAM and a 4MHz oscillator - divided by 4 for a 1MHz basic instruction cycle.

    Oscillator! You were lucky to have an oscillator! We had to generate a clock signal by toggling a switch on and off as fast as we could!

     

    We used to dream of having a switch!  In my day we had to complete the circuit by touching two bare wires to our tongues!

    Wires?  Wires?  You had the sheer brazen decadent luxury of wires?  Pah!  When I wer't lad, we 'ad to carry electrons from't anode to't cathode one-by-one using a very very small pair of tweezers, and if we sneezed and dropped one, Father would beat us to death with a rusty porcupine.  And we called ourselves lucky! 

    Wait, you had [b]tweezers[/b], [b]anodes[/b] and [b]cathodes[/b]? When I started my first job, I got a rusty nail magnetized by the earths magnetic field with which I had to lure electrons through the individual components of the circuit! It wasn't pre-wired ofcourse but I was allowed to use a crayon to draw lines between the components on the floor.



  • @dtech said:

    @DaveK said:

    @dwilliss said:

    @autorelease said:

    @SenTree said:
    @spxza said:

    Resources are limited (3MB storage, 1.5MB memory, and runs at around 12MHz).

    Luxury ! On my current project, the main processor has 256kB flash, 64kB RAM, but at least a decent clock rate. The auxiliary processors have 32kB flash, 1536 bytes RAM and a 4MHz oscillator - divided by 4 for a 1MHz basic instruction cycle.

    Oscillator! You were lucky to have an oscillator! We had to generate a clock signal by toggling a switch on and off as fast as we could!
     

    We used to dream of having a switch!  In my day we had to complete the circuit by touching two bare wires to our tongues!

    Wires?  Wires?  You had the sheer brazen decadent luxury of wires?  Pah!  When I wer't lad, we 'ad to carry electrons from't anode to't cathode one-by-one using a very very small pair of tweezers, and if we sneezed and dropped one, Father would beat us to death with a rusty porcupine.  And we called ourselves lucky! 

    Wait, you had tweezers, anodes and cathodes? When I started my first job, I got a rusty nail magnetized by the earths magnetic field with which I had to lure electrons through the individual components of the circuit! It wasn't pre-wired ofcourse but I was allowed to use a crayon to draw lines between the components on the floor.

     

    Wow, you had the earth's magnetic field?! Back in the day, all we had was a bunch of rocks and gas floating around in space...



  • @CrazyBomber said:

    @dtech said:

    @DaveK said:

    @dwilliss said:

    @autorelease said:

    @SenTree said:
    @spxza said:

    Resources are limited (3MB storage, 1.5MB memory, and runs at around 12MHz).

    Luxury ! On my current project, the main processor has 256kB flash, 64kB RAM, but at least a decent clock rate. The auxiliary processors have 32kB flash, 1536 bytes RAM and a 4MHz oscillator - divided by 4 for a 1MHz basic instruction cycle.

    Oscillator! You were lucky to have an oscillator! We had to generate a clock signal by toggling a switch on and off as fast as we could!
     

    We used to dream of having a switch!  In my day we had to complete the circuit by touching two bare wires to our tongues!

    Wires?  Wires?  You had the sheer brazen decadent luxury of wires?  Pah!  When I wer't lad, we 'ad to carry electrons from't anode to't cathode one-by-one using a very very small pair of tweezers, and if we sneezed and dropped one, Father would beat us to death with a rusty porcupine.  And we called ourselves lucky! 

    Wait, you had tweezers, anodes and cathodes? When I started my first job, I got a rusty nail magnetized by the earths magnetic field with which I had to lure electrons through the individual components of the circuit! It wasn't pre-wired ofcourse but I was allowed to use a crayon to draw lines between the components on the floor.

     

    Wow, you had the earth's magnetic field?! Back in the day, all we had was a bunch of rocks and gas floating around in space...

     

    *spit* Rocks and gas?  Rocks and gas are for jessies.  In my day we had to make do without the universe!  First we had to will ourselves into existence, then Father would make us sit in the pre-Big-Bang quantum void and repeatedly open closed schrodinger boxes hundreds of quindecillions of times until finally we found an electron inside one, and that happens a lot less often than the 50-50 odds you get with that fookin' cat, let me tell you! 



  • @Helix said:

    Microchip now own Hitech.

    So now not only to they make crap micros they also make poor compilers......

    Tell me about it. They announced this the day after I'd decided to go for the Microchip compiler instead of the Hi-Tech one !
    We've had lots of subtle problems with Hi-Tech, including forgetting to reset the RAM bank select flags on exiting from some, not all, cases of a switch. The next pass through the state machine then wandered off into limbo. (No, turning optimisation off didn't fix it).
    Microchip's own PIC18 compiler looks better, at least at first glance, and is verified with the Plum Hall suite (Hi-Tech use their own test suite - no comment). I'm just hoping they don't scrap it.



  • @DaveK said:

    @CrazyBomber said:

    @dtech said:

    @DaveK said:

    @dwilliss said:

    @autorelease said:

    @SenTree said:
    @spxza said:

    Resources are limited (3MB storage, 1.5MB memory, and runs at around 12MHz).

    Luxury ! On my current project, the main processor has 256kB flash, 64kB RAM, but at least a decent clock rate. The auxiliary processors have 32kB flash, 1536 bytes RAM and a 4MHz oscillator - divided by 4 for a 1MHz basic instruction cycle.

    Oscillator! You were lucky to have an oscillator! We had to generate a clock signal by toggling a switch on and off as fast as we could!
     

    We used to dream of having a switch!  In my day we had to complete the circuit by touching two bare wires to our tongues!

    Wires?  Wires?  You had the sheer brazen decadent luxury of wires?  Pah!  When I wer't lad, we 'ad to carry electrons from't anode to't cathode one-by-one using a very very small pair of tweezers, and if we sneezed and dropped one, Father would beat us to death with a rusty porcupine.  And we called ourselves lucky! 

    Wait, you had tweezers, anodes and cathodes? When I started my first job, I got a rusty nail magnetized by the earths magnetic field with which I had to lure electrons through the individual components of the circuit! It wasn't pre-wired ofcourse but I was allowed to use a crayon to draw lines between the components on the floor.

     

    Wow, you had the earth's magnetic field?! Back in the day, all we had was a bunch of rocks and gas floating around in space...

     

    *spit* Rocks and gas?  Rocks and gas are for jessies.  In my day we had to make do without the universe!  First we had to will ourselves into existence, then Father would make us sit in the pre-Big-Bang quantum void and repeatedly open closed schrodinger boxes hundreds of quindecillions of times until finally we found an electron inside one, and that happens a lot less often than the 50-50 odds you get with that fookin' cat, let me tell you! 

    At the risk of eternal ostracism: http://www.xkcd.com/505/ (now diving for cover...)



  • @SenTree said:

    @DaveK said:

    @CrazyBomber said:

    @dtech said:

    @DaveK said:

    @dwilliss said:

    @autorelease said:

    @SenTree said:
    @spxza said:

    Resources are limited (3MB storage, 1.5MB memory, and runs at around 12MHz).

    Luxury ! On my current project, the main processor has 256kB flash, 64kB RAM, but at least a decent clock rate. The auxiliary processors have 32kB flash, 1536 bytes RAM and a 4MHz oscillator - divided by 4 for a 1MHz basic instruction cycle.

    Oscillator! You were lucky to have an oscillator! We had to generate a clock signal by toggling a switch on and off as fast as we could!
     

    We used to dream of having a switch!  In my day we had to complete the circuit by touching two bare wires to our tongues!

    Wires?  Wires?  You had the sheer brazen decadent luxury of wires?  Pah!  When I wer't lad, we 'ad to carry electrons from't anode to't cathode one-by-one using a very very small pair of tweezers, and if we sneezed and dropped one, Father would beat us to death with a rusty porcupine.  And we called ourselves lucky! 

    Wait, you had tweezers, anodes and cathodes? When I started my first job, I got a rusty nail magnetized by the earths magnetic field with which I had to lure electrons through the individual components of the circuit! It wasn't pre-wired ofcourse but I was allowed to use a crayon to draw lines between the components on the floor.

     

    Wow, you had the earth's magnetic field?! Back in the day, all we had was a bunch of rocks and gas floating around in space...

     

    *spit* Rocks and gas?  Rocks and gas are for jessies.  In my day we had to make do without the universe!  First we had to will ourselves into existence, then Father would make us sit in the pre-Big-Bang quantum void and repeatedly open closed schrodinger boxes hundreds of quindecillions of times until finally we found an electron inside one, and that happens a lot less often than the 50-50 odds you get with that fookin' cat, let me tell you! 

    At the risk of eternal ostracism: http://www.xkcd.com/505/ (now diving for cover...)

    I like XKCD, but that's a one-upmanship FAIL.  He had rocks!  :-)


  • @DaveK said:

    @SenTree said:
    @DaveK said:

    spit Rocks and gas?  Rocks and gas are for jessies.  In my day we had to make do without the universe!  First we had to will ourselves into existence, then Father would make us sit in the pre-Big-Bang quantum void and repeatedly open closed schrodinger boxes hundreds of quindecillions of times until finally we found an electron inside one, and that happens a lot less often than the 50-50 odds you get with that fookin' cat, let me tell you! 

    At the risk of eternal ostracism: http://www.xkcd.com/505/ (now diving for cover...)

    I like XKCD, but that's a one-upmanship FAIL.  He had rocks!  :-)

    OK, you win ! :(



  •  @DaveK said:

    This is, of course, just the bog-standard "x = !!x" formulation used to normalize a bool into the 0-1 range.  With a superfluous assignment in the zero case.

     v's value is assigned from this function:


    2.6.2 GPIO.getIOvalue(GPIOnumber)
    Gets input or output value of a GPIO. Input parameter GPIOnumber is a Python integer which is the
    number of the GPIO. Return value is a Python integer which is -1 if an error occurred otherwise is input or output value. It is
    0 or 1.
     

    Can anyone see it? These GPIO's input 1 when an external device is on, and 0 when it is off.



  • @spxza said:

     @DaveK said:

    This is, of course, just the bog-standard "x = !!x" formulation used to normalize a bool into the 0-1 range.  With a superfluous assignment in the zero case.

     v's value is assigned from this function:


    2.6.2 GPIO.getIOvalue(GPIOnumber)
    Gets input or output value of a GPIO. Input parameter GPIOnumber is a Python integer which is the
    number of the GPIO. Return value is a Python integer which is -1 if an error occurred otherwise is input or output value. It is
    0 or 1.
     

    Can anyone see it? These GPIO's input 1 when an external device is on, and 0 when it is off.

    Ah, then I should say

     @DaveK said:

    This is, of course, just the bog-standard "WTF? = WTF!!" formulation used to normalize a true-false-file-not-found ternary into the 0-1 range.  With a superfluous assignment in the zero case.




  •  My old boss used to write software that ran on the space shuttle and the Venus Mariner probes. Some of the sub-modules had watchdog timers which would reboot the board 100 times per second. This was because of the intense radiation in space -- it was cheaper to simply reboot than to build every board with full radiation hardening. So you had to design your code so that you could boot up, retrieve values from hardened storage, perform the computation, write them back to storage, and shut down, within 1/100th of a second, because that watchdog was going to reboot you anyway, provided you hadn't already crashed from a stray cosmic ray.

     Software in outer space has some interesting constraints.

     



  • @smxlong said:

     My old boss used to write software that ran on the space shuttle and the Venus Mariner probes. Some of the sub-modules had watchdog timers which would reboot the board 100 times per second. This was because of the intense radiation in space -- it was cheaper to simply reboot than to build every board with full radiation hardening. So you had to design your code so that you could boot up, retrieve values from hardened storage, perform the computation, write them back to storage, and shut down, within 1/100th of a second, because that watchdog was going to reboot you anyway, provided you hadn't already crashed from a stray cosmic ray.

     Software in outer space has some interesting constraints.

     

    Reframed, in the current idiom:

    NASA used to get me to fix problems they had with space shuttle software, that my boss gave them. 

    Then I learned about hard radiation flipping bits in 1/100th of a second and what happened next,

    and now I don't have to fix the space shuttle any more.

    Long live O-Rings! 

     


Log in to reply