User talk:Zaher s
Execution of C++ programs in shortest possible time (nanoseconds or milliseconds)
Zaher S Al-Hashami Muscat - Sultanate of Oman Research Center ac.res@hotmail.com
ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ Abstract Many programmers have multiple ways for programming in programming languages. Can reach the same result for a particular function works in a short time compared with the same function in another program to ensure access the result in a longer time. Difference among good and excellent programmers is reaching to the last result in short time and short way algorithm. In C++ programs could measure execution of some programs with Nanoseconds and Milliseconds. In this article loop and condition statements in C++ programming could measure them execution in little Nanoseconds or in Milliseconds. Comparison and approach between some programs algorithms are the method used in this paper. Execution of time taking of some C++ programs is depending on the algorithm of the program and statement long time taking. Preprocessor has role in speedup some Nanoseconds of execution of some C++ programs as compile time (convert to source code). These preprocessor statements like #define and 3include preprocessor directives. There are many circumstances control the speed of some programs execution. Architecture of programming progress as well as hardware has role in speed of execution. The algorithm used in some C++ programs and execution of some functions is the topic of this article. Keywords: Milliseconds, Nanoseconds, Comparison, preprocessor, #define, #include ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ
Acknowledgment
Thank to everyone help me and was helpful to finish this simple article. I hope that this article is very good beginning for me to go through scientific work. I hope to do more researches in IT and CS scope.
1. Introduction As known, computer operates on two fundamentally different time scales. First level which is execute instructions at a rate of one or more per clock cycle, where each clock cycle requires only around one nanosecond. These nanoseconds time is executed on basis of algorithm of the program or the way of a precise or long way. The second is on a macroscopic scale, the processor must respond to external events. Commonly inside computer multiprogramming systems and according to Ugur Halıcı, 2007, processes are performed in a pseudo parallelism as if each process has its own processor. Also based on this author there is only one processor but it switches back and forth from process to process. So when he said execution of a process, he meant that the processor’s operations on the process like changing its variables and etc. Input data output (I/O) data work means the interaction of the process with the I/O operations like reading from x file or writing to x file. Based on these definitions, he classify programs as tow types, the first is processor bound program which is a program having long processor bursts or can call it execution instants. The second is bound program a program having short processor bursts [1], (Halıcı2007). According to Roberto Cipolla, 2004, that C++ programs go through three main phases during development: editing (writing the program), compiling (i.e. translating the program to executable code and detecting syntax errors) and running the program and checking for logical errors (called debugging). The editing phase first phase consists of editing a file by typing in the C++ program with a text editor and making corrections if necessary. The program is stored as a text file on the disk, usually with the file extension .cc to indicate that it is a C++ program. Next phase is compile time; the compiler translates the C++ program into machine language code which it stores on the disk as a file. A linker then links the object code with standard library routines that the program may use and creates an executable image which is also saved on disk, usually as a file with the file name without any extension. The last phase is execution executable is loaded from the disk to memory and the computer's processing unit executes the program one instruction at a time [2], (Cipolla, 2004). One second is equal 1000 milliseconds and also equal 1000,000,000 nanoseconds. Short nanoseconds executions of some programs comparing with others that are longer are doing same function. There are many Algorithms could form in C++ programs and could programming it by them in precise and long ways. Short time execution in nanoseconds and milliseconds is the goal of this research and comparing these programs and their scripts. 1.1 Compiling in C++
When writing a C++ program, the next step is to compile the program before running it. The compilation is the process which convert the program written in human readable language like C, C++ etc into a machine code, directly understood by the Central Processing Unit. There are many stages involved in creating a executable file from the source file. The stages include Preprocessing, Compiling and Linking in C++. This means that even if the program gets compiled, it may result in not running as errors may arise during the linking phase. Hence most IDE (Integrated Development Environment ) like Eclipse, Geany etc consider the term build for transforming source code file to an executable file, there are two phase define compilation in C++ programming: first preprocessing phase and second compilation phase.
In the preprocessing phase, the preprocessor changes the Preprocessing according to the directives mentioned (that starts with # sign). The C++ preprocessor takes the program and deals with the # include directives and the resulting program is pure C++ program. For example, in C++ program #include<iostream> will tell the preprocessor to read all the contents of the iostream header file and include the contents into the program and generate the separate C++ program file. C++ supports many preprocessor directives like #include, #define, #if, #else etc. Then come compilation phase which translates the program into a low level assembly level code. The compiler takes the preprocessed file ( without any directives) and generates an object file containing assembly level code. Now, the object file created is in the binary form. In the object file created, each line describes one low level machine level instruction . The conversion to assembly language is important as it is common output language for many compilers of different high-level languages.
Also there is an assembly phase which converts these object files in assembly code into machine level instructions and the file created is a re-locatable object code. Hence, the compilation phase generates the re-locatable object program and this program can be used in different places without have to compile again [3], (Prashant,2013). 2. Comparing 1 1.2 Program 1
- include <iostream.h>
void main() {int d1, d2, d3, d4, sum = 0 ; float average ; cout << "Enter Four Degree : " ; cin >> d1 >> d2 >> d3 >> d4 ; sum =(d1+d2+d3+d4) ; average = sum/4 ; cout << endl ; cout << "The Average of Student is " << average << endl ;}
This C++ program is about average program for calculating the average of four values with usual stages, starting with entering the four numbers d1,d2,d3,d4, then sum them by sum function. Then, come the last function to get average for calculating function which is take average = sum/4 statement. In the last statement in this average example of C++ program. Last statement is cout the average after indentify it. In this program there are some nanoseconds spent because of extra statements written must execute and that could precise to program 2. This program below spends approximately 10,000,000 nanoseconds (0.01 seconds) for execution, which measures clock start and end, which is example for some measured C++ programs that executed [4]:
- include <time.h>
- include <iostream>
using namespace std; int main() {clock_t start, end; start = clock(); //perform calculations for which performance needs to be checked end = clock(); cout << "Time required for execution: " << (double)(end-start)/CLOCKS_PER_SEC << " seconds." << "\n\n"; return 0;
2.2 Program 2
- include <iostream.h>
void main() {int d1, d2, d3, d4 ; cout << "Enter Four Degree : " ; cin >> d1 >> d2 >> d3 >> d4 ; cout << endl ; cout << "The Average of Student is " << (d1+d2+d3+d4)/4 ; This program is the same of above program which calculate average of d1, d2, d3, and d4 numbers with precise statements. Example sum = (d1+d2+d3+d4) this statement is not mentioned in program2 while it is exited in program1, also this statement average = sum/4, not mentioned in program 2. These two statements in program1 are precise in the last statement of program 2. Nanoseconds execution approximate time of program2 is less that the execution time o program1. These two programs are example of how to short time execution of C++ programming by short concise statements. Also in the C++ below program of stopclock_example.cpp spends approximately 0.832 seconds (832 000 000 nanoseconds or 832 milliseconds)[5]:
- include <boost/chrono.hpp>
- include <cmath>
int main() {boost::chrono::system_clock::time_point start = boost::chrono::system_clock::now();
for ( long i = 0; i < 10000000; ++i ) std::sqrt( 123.456L ); // burn some time boost::chrono::duration<double> sec = boost::chrono::system_clock::now() - start; std::cout << "took " << sec.count() << " seconds\n"; return 0;}
In this program the loop statement (for) is took some time before execution during calculating the duration in seconds 0.832 seconds.
3. Comparing 2 1.3 Program 1 // custom countdown using while
- include <iostream.h>
int main () {int n ; cout << "Enter the starting number : " ; cin >> n ; while (n>0) {cout << n << ", " ; --n;} cout << "FIRE!" ; return 0 ;} In program 1 above in comparing 2 there is execution of while condition statement till a stated case (n>0). This program will execute --n (n-1) until face this condition, then it is if the condition is not attain will send this statement cout << "FIRE!". 2.3 Program 2 // operating with variables
- include <iostream.h>
int main () {int a, b; int result; a = 5; b = 2; result = a - b; cout << result; return 0;} In program 2 of comparing 2 a subtraction calculation is written. The program started with indentifying a, b and result. This identification is as int and it gives a, and b numbers. Then identify result=a-b then the last statement is: cout << result. In program 2 of comparing 2 can be seen that executing subtracting calculation directly without any loop or without any condition that must attain and that will fast execution that program 1. In program 1there is loop and while condition statement which may continue the execution till stated case (a condition). Program2 an example of short time execution of some statements in programming generally and in C++ language. In program2 milliseconds or even nanoseconds that may time spend to execution comparing with program1. By give it condition (n<1000) ++n and it approximately take 0.1 seconds (1000, 000,000 nanoseconds) or 1000 milliseconds. In program 2 the result of execution time as approximate take 0 seconds. 4. Compile time execution According to Wikipedia the encyclopedia, that the identification of compilation it is like (JIT) just in time compilation, it a method to improve the runtime performance of computer programs based on Byte Code which can call it the virtual machine code. When byte code is interpreted it executes slower than compiled machine code, which could be performed before the execution-making the program loading slow or during the execution, wikipedia.org[6], 2013. Compile time execution in some programs, execute before rune time execution. Some programs do not need and others need as instance, for replacement done in compile time execution in #define which is the preprocessor the directive substitute's statement in C++, like in the program below: 1.4 Program 1 // Filename: C6DEF2.CPP // illustrates that #define literals are not variables.
- include <iostream.h>
- define X1 b+c
- define X2 X1 + X1
- define X3 X2 * c + X1 - d
- define X4 2 * X1 + 3 * X2 + 4 * X3
main () {int b = 2; // Declares and initializes four variables. int c = 3; int d = 4; int e = X4; // Prints the values. cout << e << “, “ << X1 << “, “ << X2; cout << “, “ << X3 << “, “ << X4 << “\n”; return 0; In program 1 there are four replacements for preprocessor directives. It is executed before C++ compiling. Sometimes execution of preprocessor directive #define will cause somewhat slowing before execute C++ script in extra nanoseconds because it is compile to source code before whole C++ commands compile. 1.4 Script 2 int factorial (int n) {if (n == 0)
return 1; return n * factorial(n - 1);}
// computed at compile time const int y = factorial(0); // == 1 const int x = factorial(4); // == 24 In script2 of factorial function there is a comment before if (n == 0) return 1; return n * factorial (n - 1);}. Also this statement compiled at compile time execution that could delay execution of whole program some millisecond or some nanoseconds.
5. Discussion
David B. Stewart, 2006, in his overview of measurement techniques of execution time explains that there are many different methods exist to measure execution time, but there is no single best technique. Rather, each technique is a compromise between multiple attributes, such as resolution, accuracy, granularity, and difficulty [7].
The resolution first which is a representation of the some limitations of the timing that concern hardware. The example as Stewart a stop watch measures with a 0.01 sec resolution, while a logic analyzer may be measured with a resolution of 50 nanoseconds.
That it which conduct us for different nanoseconds of some programs off course thereupon the algorithm (precise or long way) that could execute the programs and its statement.
Then it come the accuracy which is give method of measuring, as compared to the actual time if a perfect measurement that obtained through execution of the program. If a particular measurement is repeated several times, there is usually some amount of error in the measurements that is explains also as findings of this research that for instance loop statement for or while condition.
The last technique is the granularity, it is the part of the code compiling that can be measured, and it is specified in which David B Stewart called it a subjective manner. An example for, coarse granularity methods would generally measure execution time on a per-process, per-procedure, or per-function basis.
Also that it what can call it preprocessor compiling before the C++ program execution and the delay could cause or a little slowing could cause.
6. Conclusion
C++ program execution different from program to another based on the statements, commands and compiling time taking and the statements that execute at compile time, as well as running time that is could take. In the computer the operations divided to two In general the computer operates on two different time scales. Level of microscopic is the first; which execute instructions at a one or more per clock cycle, where each clock cycle requires only around one nanosecond. The second one is on a macroscopic scale in this level the processor must respond to external events.
According to Professor Roberto Cipolla, that there are three phases of program go through development are editing, compiling and running the program and checking for logical errors (could call it debugging).
There are short ways and the algorithm of the program may reveal through programming and development to get short time results with the same precision could be in the same programs aim. Millisecond or nanoseconds different in the speed could be between two programs with non same statements. Loop (for) statements or big condition while statement as example are could also take more milliseconds than usual a+b+c or a*b/c direct, which take a little nanoseconds of execution speed.
Compile time in development which is compiling to source code take a little time before compiling of C++ program. Some commands in C++ programming like #define and #include preprocessor in the beginning of C++ programming body is must compile before C++ program compile or rune time.
7. Refernces
1- Ugur Halıcı, operating Systems, processing scheduling, 2007. Website: www.eee.metu.edu.tr/~halici/courses/442/Ch2%20Process%20Scheduling.pdf
2- Roberto Cipolla, Editing, Compiling and Executing a Simple Program, 2004. Website: www.eng.cam.ac.uk/help/languages/C++/c++_tutorial/editing.html 3- Prashant, Compiling and Linking in C++, 2013. Website: www.cplusplus.com/articles/2v07M4Gy/ 4- Website: man7.org/linux/man-pages/man7/time.7.htm,3013
5- Howard Hinnant, Beman Dawes, Vicente J. Botet Escriba, Boost C++ libraries,2008-2009. Website:www.boost.org/doc/libs/1_47_0/doc/html/chrono/users_guide.html
6- Website wiki.riteme.site/wiki/Just-in-time-compilation-,2013
7- David B. Stewart, Measuring Execution Time and Real-time Performance, 2006. Website: www.drdobbs.com/embedded-systems/measuring-execution-time-and-real-time-p/193502123
Your submission at Articles for creation: User:Zaher s/sandbox (March 17)
[edit]![](http://upload.wikimedia.org/wikipedia/commons/thumb/7/7e/AFC-Logo_Decline.svg/50px-AFC-Logo_Decline.svg.png)
Please read the comments left by the reviewer on your submission. You are encouraged to edit the submission to address the issues raised and resubmit when they have been resolved.
- If you would like to continue working on the submission, you can find it at Wikipedia talk:Articles for creation/Zaher s/sandbox.
- To edit the submission, click on the "Edit" tab at the top of the window.
- If you need any assistance, you can ask for help at the Articles for creation help desk, or on the .
contributions to Wikipedia!
- Please remember to link to the submission!
- You can also get real-time chat help from experienced editors.
Your submission at Articles for creation: User:Zaher s/sandbox (March 28)
[edit]![](http://upload.wikimedia.org/wikipedia/commons/thumb/7/7e/AFC-Logo_Decline.svg/50px-AFC-Logo_Decline.svg.png)
Please read the comments left by the reviewer on your submission. You are encouraged to edit the submission to address the issues raised and resubmit when they have been resolved.
- If you would like to continue working on the submission, you can find it at Wikipedia talk:Articles for creation/Zaher s/sandbox.
- To edit the submission, click on the "Edit" tab at the top of the window.
- If you need any assistance, you can ask for help at the Articles for creation help desk, or on the .
contributions to Wikipedia!
- Please remember to link to the submission!
- You can also get real-time chat help from experienced editors.
![]() |
Hello! Zaher s,
I noticed your article was declined at Articles for Creation, and that can be disappointing. If you are wondering or curious about why your article submission was declined please post a question at the Articles for creation help desk. If you have any other questions about your editing experience, we'd love to help you at the Teahouse, a friendly space on Wikipedia where experienced editors lend a hand to help new editors like yourself! See you there!
|
Your draft article, User:Zaher s/sandbox
[edit]![](http://upload.wikimedia.org/wikipedia/en/thumb/1/1d/Information_icon4.svg/48px-Information_icon4.svg.png)
Hello Zaher s. It has been over six months since you last edited your WP:AFC draft article submission, entitled "sandbox".
The page will shortly be deleted. If you plan on editing the page to address the issues raised when it was declined and resubmit it, simply {{db-afc}}
or {{db-g13}}
code. Please note that Articles for Creation is not for indefinite hosting of material deemed unsuitable for the encyclopedia mainspace.
If your submission has already been deleted by the time you get there, and you want to retrieve it, copy this code: {{subst:Refund/G13|User:Zaher s/sandbox}}
, paste it in the edit box at this link, click "Save page", and an administrator will in most cases undelete the submission.
Thanks for your submission to Wikipedia, and happy editing. JMHamo (talk) 18:04, 18 October 2014 (UTC)