DEV Community

sugizo
sugizo

Posted on • Updated on

Compare Programming Languages Execution Time

With stifan kristi (steve van christie) here, want to share the idea of this post is for comparing programming languages execution time with simple command that execute system command hostname to get computer hostname and for execution duration is taken by *nix time command.

The programming languages compared here are :

  • Java (*.java), need java installed to run script
  • JavaScript (*.js), need nodejs installed to run script
  • R (*.r), need r installed to run script
  • Go (*.go), need go installed to run script
  • PHP (*.php), need php installed to run script
  • Ruby (*.rb), need ruby installed to run script
  • AppleScript (*.scpt), need mac environment to run script
  • Tcl (*.tcl), need tclsh installed to run script
  • Python (*.py), need python installed to run script
  • Perl (*.pl), need perl installed to run script
  • C (*.c), need gcc installed to compile script
  • C++ (*.cpp), need g++ installed to compile script
  • Shell (*.sh), need *nix environment to run script

alternatively, you can install conda or miniconda to run : nodejs, r, go, ruby, python and perl
of course you can compile or run it with your lovely IDE, but to get the execution time duration, we use *nix time command in this topic, so better to execute everything in terminal.

*nix time command will produce 3 summarries data : real, user and sys.

  • real is realtime
  • user is user CPU time
  • sys is system CPU time

Execute Command from Terminal (without any programming language) :
time hostname

Programming Language Compared

Java

Check Version :
java -version

exec

Create File :
Exec0.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Exec0 {
    public static void main(String[] args)
    {
        try {
            Process process = Runtime.getRuntime().exec("hostname");
            StringBuilder output = new StringBuilder();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream() ) );
            String line;
            while ((line = reader.readLine() ) != null) {
                output.append(line + "\n");
            }
            int exitVal = process.waitFor();
            if (exitVal == 0) {
                System.out.println(output);
                System.exit(0);
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time javac Exec0.java
time java Exec0

ProcessBuilder

Create File :
ProcessBuilder0.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ProcessBuilder0 {
    public static void main(String[] args)
    {
        try {
            ProcessBuilder pb = new ProcessBuilder("hostname");
            Process process = pb.start();
            StringBuilder output = new StringBuilder();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream() ) );
            String line;
            while ((line = reader.readLine() ) != null) {
                output.append(line + "\n");
            }
            int exitVal = process.waitFor();
            if (exitVal == 0) {
                System.out.println(output);
                System.exit(0);
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time javac ProcessBuilder0.java
time java ProcessBuilder0

JavaScript

Check Version :
node -v

exec

Create File :
test_js_exec.js

const { exec } = require("child_process");
exec("hostname", (error, stdout, stderr) => {
    if (error) {
        console.log(`error: ${error.message}`);
        return;
    }
    if (stderr) {
        console.log(`stderr: ${stderr}`);
        return;
    }
    console.log(`stdout: ${stdout}`);
});
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time node test_js_exec.js

execSync

Create File :
test_js_execsync.js

const { execSync } = require('child_process');
const output = execSync('hostname', {encoding: 'utf-8'} );
console.log('output: ' + output);
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time node test_js_execsync.js

spawn

Create File :
test_js_spawn.js

const { spawn } = require("child_process");
const hostname = spawn("hostname");
hostname.stdout.on("data", data => {
    console.log(`stdout: ${data}`);
});
hostname.stderr.on("data", data => {
    console.log(`stderr: ${data}`);
});
hostname.on('error', (error) => {
    console.log(`error: ${error.message}`);
});
hostname.on("close", code => {
    console.log(`child process exited with code ${code}`);
});
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time node test_js_spawn.js

R

Check Version :
Rscript --version

system

Create File :
system.r

system("hostname")
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time Rscript system.r

system2

Create File :
system2.r

system2("hostname", stdout = TRUE, stderr = TRUE)
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time Rscript system2.r

sys

Install Package :
r
install.packages("sys")
any(grepl("sys", installed.packages() ) )
q()

Create File :
sys.r

out <- sys::exec_internal('hostname')
sys::as_text(out$stdout)
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time Rscript sys.r

processx

Install Package :
r
install.packages("processx")
any(grepl("processx", installed.packages() ) )
q()

Create File :
processx.r

out <- processx::run('hostname')
out$stdout
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time Rscript processx.r

Go

Check Version :
go version

Create File :
test.go

package main
import (
    "fmt"
    "os/exec"
)
func main() {
    prg := "hostname"
    cmd := exec.Command(prg)
    stdout, err := cmd.Output()
    if err != nil {
        fmt.Println(err.Error() )
        return
    }
    fmt.Print(string(stdout) )
}
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time go run test.go

PHP

Check Version :
php -v

backtick

Create File :
test_php_backtick.php

<?php
$output = `hostname`;
echo "$output";
?>
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time php test_php_backtick.php

system

Create File :
test_php_system.php

<?php
system("hostname");
?>
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time php test_php_system.php

passthru

Create File :
test_php_passthru.php

<?php
passthru("hostname");
?>
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time php test_php_passthru.php

Ruby

Check Version :
ruby -v

Create File :
test.rb

#!/usr/bin/ruby
system("hostname")
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time ruby test.rb

AppleScript

Create File :
test.scpt

do shell script "hostname"
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time osascript test.scpt

Tcl

Create File :
test.tcl

set result [exec hostname]
puts $result
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time tclsh test.tcl

Python

Check Version :
python -V

subprocess.run

Create File :
test_py_subprocess_run.py

#!/usr/bin/python
# -*- coding: utf-8 -*-
import subprocess
subprocess.run("hostname")
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time python test_py_subprocess_run.py

subprocess.Popen

Create File :
test_py_subprocess_popen.py

#!/usr/bin/python
# -*- coding: utf-8 -*-
import subprocess
subprocess.Popen("hostname")
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time python test_py_subprocess_popen.py

os

Create File :
test_py_os.py

#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
os.system("hostname")
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time python test_py_os.py

In python you can compile it first with py_compile module, then execute the compiled version in pycache directory, e.g.
time python -m py_compile test_py_subprocess_popen.py
time python __pycache__/test_py_subprocess_popen.cpython-*.pyc
time python -m py_compile test_py_subprocess_run.py
time python __pycache__/test_py_subprocess_run.cpython-*.pyc
time python -m py_compile test_py_os.py
time python __pycache__/test_py_os.cpython-*.pyc

the result for execute compiled version (.pyc) is faster than execute the source code (.py).
So, better to compile your source code when run in production environment.

But the result for compiling (python -m pycompile *.py), then execute compiled version (python *.pyc) take longer,
than execute the source code version (python *.py).

Perl

Check Version :
perl -v

backtick

Create File :
test_pl_backtick.pl

#!/usr/bin/perl
my $output = `hostname`;
print "$output";
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time perl test_pl_backtick.pl

system

Create File :
test_pl_system.pl

#!/usr/bin/perl
system("hostname");
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time perl test_pl_system.pl

exec

Create File :
test_pl_exec.pl

#!/usr/bin/perl
exec("hostname");
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time perl test_pl_exec.pl

C

Create File :
test.c

#include <stdlib.h>
int main() {
    system("hostname");
}
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time gcc test.c -o c.out
time ./c.out

C++

Create File :
test.cpp

#include <iostream>
int main() {
    system("hostname");
}
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time g++ test.cpp -o cpp.out
time ./cpp.out

Shell

TC Shell (tcsh)

Check Version :
tcsh --version

Create File :
test_tcsh.sh

#!/bin/tcsh
hostname
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time tcsh test_tcsh.sh

C Shell (csh)

Check Version :
csh --version

Create File :
test_csh.sh

#!/bin/csh
hostname
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time csh test_csh.sh

Z Shell (zsh)

Check Version :
zsh --version

Create File :
test_zsh.sh

#!/bin/zsh
hostname
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time zsh test_zsh.sh

Korn Shell (ksh)

Check Version :
ksh --version

Create File :
test_ksh.sh

#!/bin/ksh
hostname
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time ksh test_ksh.sh

Bash Shell (bash)

Check Version :
bash --version

Create File :
test_bash.sh

#!/bin/bash
hostname
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time bash test_bash.sh

Bourne Shell (sh)

Check Version :
sh --version

Create File :
test_sh.sh

#!/bin/sh
hostname
Enter fullscreen mode Exit fullscreen mode

Execute File from Terminal :
time sh test_sh.sh

Result

yeah, the result for all of this tests is not shown here because time execution result will different when test on other devices.
Author won't give opinion about any programming language. you can try it yourself.

Discussion (0)