Optimize code for speed and power consumption

Go To Last Post
14 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In a project, I want my code to be: 1)Accurate 2)fast 3)Power efficient (sorted by priority)

I can use assembly but it is a little hard.

Currently I'm using -O3 optimization level. But it increases program memory usage a lot (-Os: 892B, -O1: 968B, -O3: 1500B) and it means more instructions to run. And more instructions = more power consumption.

How much does -O3 increase the performance? Does it worth? Which level is the best for my application?

This topic has a solution.

Slow and Steady!

Last Edited: Mon. Jan 6, 2020 - 04:39 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

1)Accurate

Well, you need to supply many more details! 

Power efficient?  How much power is the rest of the circuit using & wasting?  The AVR may be a drop in the pond---you could be losing 1 watt in some power driver transistor, or 75mW in some voltage divider.   You can possibly put the AVR in sleep mode and save a lot.  Where is your schematic?

 

In a project ​​​​​​...

Which level is the best for my application?

Since you provide no details, it is hard to say!

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Apart from SLEEP the AVR is always fetching and executing opcodes and they all use the same power.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry. you are right.

The main power consumer is MCU. I used sleep modes in my code everywhere I could (ADC conversions, timer waits, wait for UART data...).

Note: Code size isn't very important.

 

Slow and Steady!

Last Edited: Sun. Jan 5, 2020 - 05:03 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The main power consumer is MCU

Where is your schematic?...sometimes you may think that, then realize some pulldown or base drive is wasting as much as the micro.  

What is the source for your power?

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Don't care about rest of my circuit. I want MCU to consume least power. Be sure it's not going to be a drop in the pond.

The code doesn't have many logical or arithmetic operations. Most of my code is setting pins, initializing timer...

Just tell me: How much does -O3 increase the performance? Does it worth?

Slow and Steady!

Last Edited: Sun. Jan 5, 2020 - 05:57 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It must be measured for every program and compiler version 

You must run it in simulator or add some debugging to count real cycle count used 

-O3 produce bigger code mostly because it do things like unroll short loops (so make code bigger, but avoid jumps so total execution is faster), precalculate many values and keep them in memory to save time later and so on

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Look at the data sheet.  For example, the AVR 0-series datasheet says "For best power consumption, disable the input of unused pins and pins that are used as analog inputs or outputs."  CPU clock speed matters.  Using external crystal matters.

 

EDIT: also defining functions in a file as "static" when possible can save a LOT of code (and execution time).

Last Edited: Sun. Jan 5, 2020 - 06:27 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Good idea; but there is a problem: Atmel studio simulator doesn't support UART communication. However, I'll try your suggestion.

Slow and Steady!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

On using "static", I'm writing some code to perform TWI on the mega4809.  I have one version that has functions that deal with IO registers and one that uses a pointer to a structure:

// from "main1.c":
STATIC void twi_master_reset()
{
  TWI0.MCTRLA |= 0x01;			/* enable master */
  TWI0.MCTRLB = 0x40;			/* flush */
  twm_mode = TWM_M_READY;
}
============================================================
// from "main2.c":
STATIC void twi_master_reset(twm_t *twm) {
  twm->io->MCTRLA |= 0x01;		/* enable master */
  twm->io->MCTRLB = 0x40;		/* flush */
  twm->mode = TWM_M_READY;
}

The file main1.c works w/ globals and the file main2.c works with pointer to structure.   I compile with "-DSTATIC="  and "-DSTATIC=static".   Note how using static dramatically reduces size (as reported by the "avr-size" command).

 

size (bytes)        w/o "static"       w/ "static"

main1.o              326                   190

main2.o              362                   164

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Don't care about rest of my circuit. I want MCU to consume least power. 

 

The first rule of optimisation is... don't. First you need to make measurements. A recent project I was working on needed to reduce its power consumption. What was found was that the bulk of the consumption was in idle and that the voltage regulator was consuming a significant portion of that. Changing the voltage regulator gained a significant amount of run time.

 

So the moral of the story is to understand where the power is being used. Once this is understood, you should be able to estimate what gains are possible and whether they are worthwhile. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I did some tests using break points. When using -O3 program hit the break points in less clock cycles.
And it means it will enter the sleep mode sooner. So I stick to -O3. Thanks everyone.

Slow and Steady!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For really to help we need to know witch AVR you use.

And a short description of it's job (to know possible sleep modes etc.) 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

pajuhesh80 wrote:
... Atmel studio simulator doesn't support UART communication.
Does have logging.

Haven't tried the Data Visualizer extension with the simulator.

 

Example Stimuli Session - - Simulator

[1/3 page]

Session 2; Simple AVR Logging

File Logger | Data Visualizer User's Guide

 

"Dare to be naïve." - Buckminster Fuller