Link to home
Start Free TrialLog in
Avatar of rwniceing
rwniceing

asked on

running time between linux-script and C++ on linux

Dear Experts,

Linux script is more or less like interpreted scrip as javascript but working for different platform such as one is for browsers and other is for  linux system script, Right ? Is linux-script  based on Assembly , C or C++ language  ?
I tried one benchmark code speed testing for "fop loop"  with linux  "time" command ,
The execution time of the C++ program is  around 0.01 seconds(11 ms) but for the linux script,
the time is  34 seconds that is totally greater than C++.

Do you have idea to explain the different result ? Or the time difference is depended on
some linux system  differnt setting on using  memory for script and binary program  ?
 And attached just simple example code  for "for loop" program with iteration of 2e6

Please advise

Rwniceing

Bash shell for loop with 2e6;
#!/bin/bash
for ((i=1;i<=200000000;i++))
do
a="$[1234+$5678+$i]"
b="$[1234*5678+$i]"
c="$[1234/2+$i]"
done

Open in new window

C++ for loop with 2e6
int main(){
int max = 2e6;
int a,b,c;
	// CODE YOU WANT TO TIME
	int start = getMilliCount();
	for (int i = 0; i < max; i++) {
    a = 1234 + 5678 + i;
    b = 1234 * 5678 + i;
	c=1234/2+i;
   }
return 0;
}

Open in new window

Avatar of Manfred Bertl
Manfred Bertl
Flag of Germany image

Linux script is an interpreter language, while C++ Code ist compiled into machine code. Therefor C++ programs are that faster to run, because they run w/o that layer of the interpreter.
Avatar of rwniceing
rwniceing

ASKER

So  for ((i=1;i<=200000000;i++)) is just  command more than language in linux-script, Right ? How linux interprete the "for loop", (probably it won't translate to assembly langauge since it wont be compiled or linked) and I think linux system treat the "for loop" as command and directly call its related  binary code to achieve the result, Right ? If so, the speed might not be too slow on linux-script.

for example,
$ x="a"
$ echo x
a
The Script in Linux runs within your shell (mostly bash, i guess). The Shell interprets the commands, hast to translate them and executes them.
And it might be in shell linux script, it will create a lot of child process and memory to to complete task which is not as efficient as C++ binary code process. But for c++ binary code after complied and linked, it just create single thread or process and  the memory is used less than script .
ASKER CERTIFIED SOLUTION
Avatar of Manfred Bertl
Manfred Bertl
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Optional question: Do you have any article to talk about this topic ?
No, i'm very sorry, i have no article about that. There might be tons of articles, when you search for performance tuning when coding scripts or applications. So i'm afraid, i have to point you to the search engines available.
the script's advantage and disadvantage mentioned in this link and some wording for
comparision from conventional program  langugage  at http://en.wikipedia.org/wiki/Shell_script

Thanks for your reply
Your are very welcome! Thanks for accepting the answer.

regards,
fred