Power efficiency in C while loops & polling [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I've heard that lets say:
while(1){
i = !2;
wait(1);
}
is power efficient. Does this while loop stop at i != 2 and is therefore not polling? Let's say:
while(x == 3){
if(c == 3){
x = 4;
}
wait(1);
}
Does this follow a similar concept or is i = !2 a procedure that must be met in order to continue the while loop? Would you say that this is just as power efficient? Is the second example similar to the first in terms of power efficiency?
An example i've been shown using bad power efficient polling is:
while (x == 3) { }

The important thing from an efficiency standpoint is that the code doesn't just continually cycle. In your example, presumably the wait() function is returning control to your OS so that it can immediately dispatch another task.
In short, yes, your second example is power efficient as well, assuming wait() returns control to the the OS.

Related

Math operation in the test expression of the `for` loop - perfomance, optimisation [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I am interested, which of these forms of the for loop test expression is more right (from the point of the performance and the good code practice):
for(i = 0; i < size - 1; i++) {
do something
}
or
int decreased_size = size - 1;
for(i = 0; i < decreased_size; i++) {
do something
}
Is the test expression size - 1 calculated every time in the first example or does the compiler optimize it to the constant value, so there is no need to creating an additional variable decreased_size?
I was creating an additional variable all the time, but now, looking at the others solutions on the Codeforces, I doubts - whether it makes sense?
Compiler: GCC version 5.4.0 20160609
No one makes more sense than other. Indeed, with optimization, it produces same code : https://godbolt.org/g/vzVJVF
Secondly, time consumed by size-1 is, in most case, negligible vis-a-vis of time consumed by action in loop, so optimize this part has a really small effect on system.
In conclusion, optimize only when it's needed (so you see that there is an time/memory issue). In every day, prefer a readable, easy to understand code.
I agree with #Garf365. If you also look at https://www.tutorialspoint.com/assembly_programming/assembly_loops.htm you will see that loop count is loaded into register before loop starts and so size-1 has to be computed and loaded only once.

Time complexity for recursive function with random input [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm a beginner in C programming so i need some help for my time complexity function.
int function(int n)
{ if (n <= 1)
return n;
int i = random(n-1);
return test(i) + test(n - 1 - i);
}
I don't know how to deal with this problem because of the random function which have O(1) complexity which return randomly numbers.
I don't know how to deal with this problem because of the random function which have O(1) complexity which return randomly numbers.
Well clearly you treat the random(n-1) call itself as a simple (constant time) call. Taken in isolation that is straight forward. The interesting thing is what effect the value returned by the call has on the performance.
Hint: first consider the best-case and worst-case performance for the algorithm.
Hint: for the purposes of analysis, consider a hypothetical version of random which generates a number sequence that is the antithesis of random numbers :-)

Using memcmp Vs. == in c (embedded) and unexpected errors [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I keep hearing that using == operator to compare chars can cause some problems.
My question is - what kind of problems?
For example, is there any difference between using:
if (Text[0] == 'A') { ; }
and
if (!memcmp(Text, "A", 1)) { ; }
You've blurred together two separate ideas.
There's nothing wrong with
if (Text[0] == 'A')
It'll do just what you want.
What you probably heard, and what you can't do, is
if (Text == "A")
That will virtually always come out false, even if Text contains "A".
If you want to compare strings, you generally use strcmp:
if(strcmp(Text, "A") == 0)
Of course this only works on properly-formed, null-terminated strings.
Finally, while you can certainly do
if (!memcmp(Text, "A", 1))
if you really want to, it's a strange and potentially inefficient usage, which doesn't buy you anything.

Program with while 1 hogs the processor [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am currently experimenting to find out why a process with while 1 hogs the processor. Here is the sample of my code.
int *a = NULL;
while(1)
{
a = (int*)malloc(10000);
std::cout << "Ptr to allocated memory: " << a << std::endl;
}
When I run the "top" command, it is at the top as long as it is executing. Can someone please help me understand the reason? If someone with assembly level knowledge could also provide an answer it would be really nice.
while(1) will run something forever (or until there is a break). The code inside the loop will be executed constantly, hogging much of the processor.
This is like a parent with a very needy child- your program will get all of the attention, without letting any of the other programs get what they need.

Casting improvement for microcontroller [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is there a way to improve this line of code? I think this conversion slows down my program. I think something like bit shifting can be used, by I am not sure.
(uint16_t)(0.8*(float)(Value) ?
EDIT: I need to program the atmega8 microcontroller. My teacher said that this line of code would require more processing power and that there is a simpler way of doing this with bit shifting.
(I'm assuming that Value is an int as well.)
0.8 times x is the same as (4 times x) divided by five. Multiplying an integer by a power of 2 can be done very quickly with a bit-shift. You can do this explicitly if you know how, but any modern compiler will automatically optimize int x = ...; x *= 8; to a bit-shift for you, so you don't need to worry about it. Details about how to do this by hand are widely available if you are interested.
So one thing to try is (Value * 4) / 5.

Resources