pragmatism first

Hello World! - C family languages comparison

Hello World! with command line arguments printing (C, C++, C#, Java, JavaScript, PHP).

Introduction

Section for each language contains: source code, compilation command, launch command.

Sections: C | C++ | C# | Java | JavaScript | PHP .

Benchmarks contains: executable file sizes, execution times, memory consumptions.

Benchmarks: summary table | testing environment .

Code line "wait / pause until key pressed" is commented (used for memory usage measurement to prevent termination).

Output format convention

Each solution formats output in same way.

No arguments.
Hello World!
Arguments 0.
One argument.
Hello World!
Arguments 1:
- "first".
Multiple arguments.
Hello World!
Arguments 3:
- "first",
- "second",
- "last".

C

Source code file: main.c
#include <stdio.h> int main(int argc, char ** argv){ printf("Hello World!\n"); char * eolc = argc != 0 ? ":" : "."; printf("Arguments %d%s\n", argc, eolc); for (int i = 0; i < argc; i++){ char * eola = i + 1 != argc ? "," : "."; printf("- \"%s\"%s\n", argv[i], eola); } //getchar(); return 0; }
Compilation command:
gcc main.c -o c.app
Run command:
./c.app "test" "123" "abc"

C++ (CPP)

Source code file: main.cpp
#include <iostream> using namespace std; int main(int argc, char ** argv){ cout << "Hello World!" << endl; string eolc = argc != 0 ? ":" : "."; cout << "Arguments " << argc << eolc << endl; for (int i = 0; i < argc; i++){ string eola = i + 1 != argc ? "," : "."; cout << "- \"" << argv[i] << "\"" << eola << endl; } //getchar(); return 0; }
Compilation command:
g++ main.cpp -o cpp.app
Run command:
./cpp.app "test" "123" "abc"

C# (C Sharp)

Source code file: main.cs
using System; namespace Console1 { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); int argc = args.Length; String eolc = argc != 0 ? ":" : "."; Console.WriteLine("Arguments " + argc + eolc); for (int i = 0; i < argc; i++) { String eola = i + 1 != argc ? "," : "."; Console.WriteLine("- \"" + args[i] + "\"" + eola); } //Console.Read(); } } }
Compilation command (generated by IDE):
C:\Windows\Microsoft.NET\Framework\v3.5\Csc.exe /noconfig /nowarn:1701,1702 /errorreport:prompt /warn:4 /define:TRACE /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Data.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll /debug:pdbonly /filealign:512 /optimize+ /out:obj\Release\Console1.exe /target:exe Main.cs Properties\AssemblyInfo.cs
Run command:
Console1.exe "test" "123" "abc"

Java

Source code file: Main.java
class Main{ public static void main(String[] args){ System.out.println("Hello World!"); int argc = args.length; String eolc = argc != 0 ? ":" : "."; System.out.println("Arguments " + argc + eolc); for (int i = 0; i < argc; i++){ String eola = i + 1 != argc ? "," : "."; System.out.println("- \"" + args[i] + "\"" + eola); } //try { System.in.read(); } catch (Exception e) { } } }
Compilation command:
javac Main.java
Run command:
java Main "test" "123" "abc"

JavaScript (JS)

Source code file: main.js
console.log("Hello World!"); let argv = process.argv; let argc = argv.length; let eolc = argc > 0 ? ':' : '.'; console.log("Arguments " + argc + eolc); for (let i = 0; i < argc; i++){ let eola = i + 1 != argc ? "," : "."; console.log("- \"" + argv[i] + "\"" + eola); } //process.stdin.on('data', key => {process.exit();});
Compilation command:
n/a (script)
Run command:
nodejs main.js "test" "123" "abc"

PHP

Source code file: main.php
<?php echo "Hello World!\n"; $argc = count($argv); $eolc = $argc > 0 ? ':' : '.'; echo "Arguments ".$argc.$eolc."\n"; for ($i = 0; $i < $argc; $i++){ $eola = $i + 1 != $argc ? "," : "."; echo "- \"".$argv[$i]."\"$eola\n"; } //readline();
Compilation command:
n/a (script)
Run command:
php main.php "test" "123" "abc"

Summary table

Language Executable file size (1) Execution time (1, 2) Memory consumption (3)
M_SIZE (VIRT) M_RESIDENT (RES) M_SHARE (SHR)
C 7.2 kB 9 ms 2.31 kB 0.58 kB 0.54 kB
C++ 12.6 kB 15 ms 4.97 kB 1.25 kB 1.16 kB
C# 4.5 kB 190 ms 1.1 MB (4)
Java 1.2 kB 593 ms 819 MB 28.06 kB 18.39 kB
JavaScript n/a 2468 ms 99 MB 24.65 kB 18.04 kB
PHP n/a 133 ms 83.13 kB 21.64 kB 17.74 kB
  1. Compiled with commented line "wait / pause until key pressed".
  2. Time of second execution (due eliminating time of pre loading dependencies from HDD to RAM). Console scripts for measurement used.
    Script uses PHP run command (analogue for C, C++, Java, JavaScript):
    date +"%T.%3N" php main.php "test" "123" "abc" date +"%T.%3N" php main.php "test" "123" "abc" date +"%T.%3N"
    Script for C#:
    @echo %time% Console1.exe "test" "123" "abc" @echo %time% Console1.exe "test" "123" "abc" @echo %time%
  3. Compiled with line "wait / pause until key pressed".
    Linux htop used. From htop(1) manual:
    M_SIZE (VIRT) The size of the virtual memory of the process. M_RESIDENT (RES) The resident set size (text + data + stack) of the process (i.e. the size of the process's used physical memory). M_SHARE (SHR) The size of the process's shared pages.
  4. Value from Windows task manager used. Image:
    Console C# .NET application Hello World! at task manager

Testing environment

Low end dual boot 32 bit PC with Intel Atom CPU, 2GB RAM:
Language App / Tool Version User manual
C gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0 gcc-7(1)
C++ g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
C# .NET Framework v2.0.50727
Java javac 11.0.11
java openjdk 11.0.11 2021-04-20
JavaScript nodejs v8.10.0 node(1)
PHP php 7.2.24-0ubuntu0.18.04.7 (cli) php7.2(1)