I am looking for an example where an algorithm is apparently changing its complexity class due to compiler and/or processor optimization strategies.
Lets take a simple program which prints the square of a number entered on the command line.
#include <stdio.h>
int main(int argc, char **argv) {
int num = atoi(argv[1]);
printf("%d\n",num);
int i = 0;
int total = 0;
for(i = 0; i < num; i++) {
total += num;
}
printf("%d\n",total);
return 0;
}
As you can see, this is a O(n) calculation, looping over and over
again.
Compiling this with gcc -S one gets a segment that is:
LBB1_1:
movl -36(%rbp), %eax
movl -28(%rbp), %ecx
addl %ecx, %eax
movl %eax, -36(%rbp)
movl -32(%rbp), %eax
addl $1, %eax
movl %eax, -32(%rbp)
LBB1_2:
movl -32(%rbp), %eax
movl -28(%rbp), %ecx
cmpl %ecx, %eax
jl LBB1_1
In this you can see the add being done, a compare and a jump back
for the loop.
Doing the compile with gcc -S -O3 to get optimizations the segment between the calls to printf:
callq _printf
testl %ebx, %ebx
jg LBB1_2
xorl %ebx, %ebx
jmp LBB1_3
LBB1_2:
imull %ebx, %ebx
LBB1_3:
movl %ebx, %esi
leaq L_.str(%rip), %rdi
xorb %al, %al
callq _printf
One can now see instead it has no loop and furthermore, no adds.
Instead there is a call to imull which multiplies the number by
itself.
The compiler has recognized a the loop and the math operator inside and replaced it by the proper calculation.
Note that this included a call to atoi to get the number. When the number exists already in the code, the complier will pre-calculate the value rather than making actual calls as demonstrated in a comparison between the performance of sqrt in C# and C where sqrt(2) (a constant) was being summed across a loop 1,000,000 times.
I am looking for an example where an algorithm is apparently changing its complexity class due...
Language: Java I am looking to create a .java class that will do the following for me: For example I search "sword" and if a line contains the text sword (in the following fomat), Then it changes the line "equipment-slot": RING, to equipment-slot": WEAPON, "id": 6605, "name": "White sword", "examine": "It's a White sword.", "equipment-slot": RING, "stackable": false,
I am looking at deriving an expression for the Gamow factor for
?-decay. I understand that the potential is the sum of the
nuclear, electric and effective potentials:
The Gamow factor is given by
if we consider only the Coulomb potential
I want to also incorporate the effective potential due to the
spin
In my textbook it states that the Gamow factor is a factor of
greater in the case where the effective potential is considered,
but I am...
Java Match the sorting algorithm with its time complexity, where n is the number of elements in the collection and k is the range of values. (This is a one-for-one match where an answer can only match one description.) Group of answer choices selection sort [ Choose ] O(n + k) O(n) to O(n^2) O(n log n) O(n^2) bubble sort [ Choose...
I am looking for help Maybe an example of what I can write about. I tend to not write as organized as I would like. I have a problem with not being able to narrow down one topic exactly and I just turn the light on scattered. I'm only required to do one page I'm just looking for examples of what I can write about and how I can get started without having to fall off topic. Research a specific...
linear programming:
where is i and m coming from in step5, from the second
image?The algorithm is in the first image
i
am not looking for a solution, i am trying to figure out what the
variable i and m stand for
The Simplex Algorithm for Maximum Tableaus - (1) The current tableau is of the form (ind. var.'s) a 2,2 ... ain 2,1 2,2 azn --(dep. var.'s) c c ... d -f (2) If b.b2,...,b-20, go to (6). Otherwise,...
I am looking for help with a PLC lab using LogixPro that will perform the common electrical function of controlling a light from two different locations (example: a long hallway where there is usually one light switch at each end). Each switch can control the light on and off. The program should be set up so that if both switches are ON or if both switches are Off, then the light will go ON, and it will go off otherwise....
Please I need a step by step explanation, as I am struggling with this: In Example 22.6 the length of a 60 W, 240 ΩΩ lightbulb filament was calculated to be 60 cm. If the potential difference across the filament is 120 V, what is the strength of the electric field inside the filament? Suppose the length of the bulb’s filament were doubled without changing its diameter or the potential difference across it. What would the electric field strength be...
I am doing a project in my physics class where we basically make a mythbusters episode on any movie or video game scene. My group is doing the scene in frozen where Anna goes to protect Elsa and freezes. How much force would be needed to break the Ice with a 1.1 kg Knightly Sword?
i am trying to work through several homework problems for a
class assignment due tonight. can anyone show me how I would work
through each one of these problems step by step? we are graded on
our methodology so any help, please and thanks greatly, explicitly
detailing the steps for solving the problem (so that I can know
where I might be messing up) would be wonderful
Dconsider the function fW):=v3/+x for XELO,27 a) absdute maximum and an absolute minimum...
I am getting an error from step 3 where it says "constructor Star in class Star cannot be applied to given types; required: no arguments found: int, int, int reason: actual and formal argument lists differ in length." please lmk why.