how to perform certain code on specific time in android?

Can someone run this code for me? My computer won't run it.?

  • include <stdio.h> double powern (double, unsigned); int main () { double sum = 0.0; unsigned i; for ( i = 1; i <= 200000000; i++) { sum += powern (i, i % 5); } printf ("sum = %g\n", sum); return 0; } double powern (double d, unsigned n) { double x = 1.0; unsigned j; for (j = 1; j <= n; j++) x *= d; return x; } To start with, download the code to your computer, save it and compile normally. Now we will run the code with the time command. We do this by typing: time ./a.out into the console. This will display the execution time of your program (the program will run for several seconds, so don't worry if doesn't pop up right away). There will be an output for REAL time [denotes how long in real-world time the program took], USER time [denotes the amount of time the program spent in the CPU, and SYSTEM time [denotes the amount of time the program spent waiting for other jobs to finish with the CPU]. Run the program a few more times this way and calculate the average USER time of your runs. Now open the register.c source code and add the keyword register to the declaration of each variable. Save and compile the code normally. Now run the program with the time command several times again and calculate the average USER time for running with this optimization method. Now go back to your register.c source code and remove the keyword register from the declaration of each variable. Save. This time we are going to try using gcc's compiler options to see if gcc can do a better job of optimizing our code. We will start with optimization type 1. Optimization type 1 is designed to shrink the amount of assembly code generated. The assumption is that if we produce less assembly code from our source code, then the CPU won't need to spend as much time loading instructions from RAM. Type the following line into the console to compile with optimization type 1: gcc -O1 register.c Note that the character after the hyphen is an uppercase "O", not a zero or a lowercase "o". Now run the program using time several times again so you can calculate an average. Next try optimization 2. This optimization type does everything from type 1 (minimize the assembly code generated), but it also takes into consideration the architecture (both of the software and the hardware) of the specific machine we are using. This takes a bit longer to compile with larger source codes, but should produce faster code. Compile with: gcc -O2 register.c Run the program again with time a few more times to calculate an average. Next try optimization 3. Optimization 3 prefers fast executing assembly code to small assembly code. By compiling you code using the fastest executing assembly statements and not caring about how many assembly statements are generated, this mode can be much faster or slower than optimization 2. If the resulting assembly code is still small enough to fit in RAM, optimization 3 code will run much faster. If the resulting assembly code is too large, than optimization 3 will run slower. This is what we refer to in Computer Science as the Size/Speed trade-off. (This is similar to the trade-off when trying to figure out how to move something heavy. You could move the heavy object in 1 load, or in multiple smaller loads. The best solution depends on how much longer it takes to move the large load compared to how quickly each small load can be moved.) Compile with: gcc -O3 register.c Run the program again with time a few more times to calculate an average. Next try optimization 3 with function loop unrolling. Function loop unrolling will unroll each of the loops in our source code so that the computer won't need to ever perform a "loop" or a function call. Essentially, this like giving the computer every statement it will need to run up front and in the correct order. This will usually create MUCH larger amounts of assembly code, but since there won't be any more loop or function statements, the code generated will be very fast. However, this runs into the same problem discussed above. If the code gets TOO big, it won't fit in RAM, so it will end up being slower since getting data from the hard-drive takes a very long time (in computer terms). Compile with: gcc -O3 -funroll-loops register.c Run the program again with time a few more times to calculate an average. Now write up a paragraph or two about what noticed with the averages of your different optimization techniques (include your average times for each run) and what you think about the size/speed trade-off that these instructions discussed.

  • Answer:

    do not know sorry...

Super at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

To run this code, you need to compile it first. This creates an executable file that your computer can run. That is the "a.out" that the text refers to. There are several command-line based free C compilers that can be found online. If you are a student, you can get Visual Studio for free from Microsoft's Dreamspark program. https://www.dreamspark.com/ Best, Joe

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.