Comparison of Lists in Microsoft C# .NET, C++, C, Java, Objective C, and Swift programming languages

How Lists are implemented in various Programming Languages can be an indicator of how mature and well developed those programming languages can be. In this blog post we will implement a list with 100 thousand consecutive integers, then output the contents of the list to the console, and time how long that takes to complete in each different programming language. We will also do this on different computing platforms, including Microsoft Windows 10, OpenBSD Unix, NetBSD Unix, Ubuntu Linux, Kali Linux, and Apple macOS Catalina. For OpenBSD Unix, I am using a free shell account from https://tilde.institute, which offers free shell accounts on their OpenBSD server. And for NetBSD Unix, I am using a free shell account from SDF at https://sdf.org. Also, for Ubuntu Linux, I am using a free shell account on https://tilde.team. And finally, I will also test some of the programs on Kali Linux, an excellent free operating system for security testing.

To access Unix and Linux shell accounts on Microsoft Windows 10, I am using the free PuTTY ssh client tool. I am also using the Termius ssh app which not only runs on Windows, but also runs on Android, iOS, Linux, and macOS.

Also in this blog post, I will detail some info about the people behind these programming languages.

Microsoft C# .NET

Our first example will be with Microsoft's C# (C Sharp) Programming Language which is a favorite of mine, I have 8 years experience working with C#, with both Web Applications and also Windows Applications. For web applications, C# with ASP.NET and MVC with Entity Framework cannot be beat for rapid development. Please look at some of my earlier blog posts for examples of using C# ASP.NET MVC with Entity Framework. I also have 5 years experience with C/C++, and over 10 years experience with web applications.

C# is a general purpose, multi-paradigm programming language initially developed by Microsoft beginning around 2000. C# encompasses strong typing, lexically scoped, imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines. [Wikipedia]

C# was designed by Anders Hejlsberg, and its development team is currently led by Mads Torgersen. The most recent version is 8.0, which was released in 2019 alongside Microsoft Visual Studio 2019 version 16.3.

Anders Hejlsberg (/ˈhaɪlzbɜːrɡ/, born 2 December 1960) is a prominent Danish software engineer and architect who co-designed several popular and commercially successful programming languages and development tools. He currently works for Microsoft as the lead architect of C# and also is a core developer on TypeScript.







When Microsoft first released C#, they also applied for C# to be a standard with ECMA, and then later an ISO standard. A Mexican software engineer named Miguel de Icaza saw this as an opportunity to create a version of C# and .NET framework that would be open source and also worked on creating a version that would run on Unix and Linux. Microsoft only originally intended for C# and .NET  to be Windows based, but did not object when Miguel de Icaza created a Unix/Linux version called MONO. Miguel de Icaza also created another open-source software platform for building iOS, Android, and Windows Mobile Apps from one C# code base. This is called Xamarin and has now become very popular for mobile app development. Xamarin is also the name of the San Francisco based company that Miguel de Icaza founded for development of various open source projects. Microsoft purchased Mono and Xamarin for a rumoured $400 to $500 million dollars in 2016. This was a surprising purchase for free, open-source software.

Satya Nadella is the current CEO of Microsoft. He earned a bachelor’s degree in electrical engineering from Mangalore University, a master’s degree in computer science from the University of Wisconsin – Milwaukee and a master’s degree in business administration from the University of Chicago. Very unlike Microsoft founder Bill Gates, Satya Nadella has embraced open-source technology and made it a critical part of Microsoft Business Strategy.










Miguel de Icaza (born November 23, 1972) is a Mexican-American programmer, best known for starting the GNOME, MONO, and Xamarin projects. Xamarin allows for the creation of iOS and Android mobile apps from one C# code base. He is also an advocate for human rights for the Palestinian people. He is currently employed by Microsoft.






In this blog post, we will create an example C# console application, first with Microsoft Visual Studio, and then run the example on Microsoft Windows 10. After that, we will then run the same C# console application on OpenBSD Unix with MONO. We will time the results on each platform and compare the results to other programming languages.

Here is the source code for our example C# list application, in copy and pastable text.

 // CSharpList.cs - By Michael G. Workman
 //
 // This example C# console application builds and outputs a list and times the results
 //
 // This application freely distributable under terms of MIT open source license

 using System;
 using System.Collections.Generic;
 using System.Linq;

 namespace CSharpList
 {
     class CSharpList
     {
         static void Main(string[] args)
         {
             // timing var
             var watch = new System.Diagnostics.Stopwatch();

             // create a list of integers
             var intList = new List();

             // loop 1 hundred thousand times and add consecutive integer numbers to list
             // time the building and output of the list
             watch.Start();
             for (int i = 0; i < 100000; i++)
             {
                 intList.Add(i);
             }

             // output contents of list
             foreach (int element in intList)
             {
                 Console.WriteLine(element);
             }
             watch.Stop();

            // get the time in seconds
            double timeSeconds = watch.ElapsedMilliseconds / 1000.0;

            // output the results
            Console.WriteLine("C# time to build and display list: " + timeSeconds + " seconds");
            Console.WriteLine("Number Elements: " + intList.Count());

            Console.ReadLine();
        }
    }
 }

We will first run this example C# application on a Dell Latitude 6420 Laptop with dual core 2.53 Ghz Intel processors and with Microsoft Windows 10 and Microsoft Visual Studio 2019 running the application in debug mode. This is the results we get:


Next, we will run the same exact C# program on OpenBSD Unix with MONO. I have an OpenBSD shell account on https://tilde.institute which I used in this case for the MONO run. To compile our C# program using MONO, we would do the following with the mcs command, followed by the mono command to run the program, both from the command line.


Here is the results after running our C# program in MONO, notice that there is a significant time difference when compared to running on Windows 10. Possibly due to the C# program being wrapped in and running from within the Visual Studio debug environment.


C++ Programming Language

Next we will use the C++ programming language to create a list of 100 thousand elements, and output the list to the console. First, we will do this with Visual Studio on Microsoft Windows 10, then run the same program on OpenBSD Unix, and then after that run the same program on NetBSD Unix, and compare the timing results. Also, we will compare the results for running the same program on a Macbook Pro with macOS Catalina, as well as on Ubuntu Linux and Kali Linux.

If you do not have access to a Unix system, There is some great options available for free Unix shell accounts available on the internet. https://tilde.institute gives interested persons a free Unix shell account on their OpenBSD server, and the free shell account comes with over 2 dozen software compilers including gcc, g++, clang, Java, and Objective C. Also another option is SDF, at https://sdf.org/, they provide free Unix shell accounts also on their NetBSD Unix server. Also, https://tilde.team provides free Ubuntu Linux shell accounts. Kali Linux is a free download. I used all of these to run this example C++ program, with the free Putty SSH tool, and also with the Termius app.

Here is the source code for our C++ program in copy and pastable text:

 // CPPList.cpp - By Michael G. Workman
 //
 // Example C++ console application to time the building and display of a list
 // and compare to C# and Java Programming Languages
 //
 // This example application freely distributable under terms of MIT open source license

 #include <iostream>
 #include <list>
 #include <ctime>

 using namespace std;

 int main()
 {
    // declare list of integers
    list<int> intList;

    // loop through adding 100 thousand integers to the list
    // then display the contents of the list
    // and then time the total amount of time to complete
    clock_t start = clock();
    for (int i = 0; i < 100000; i++)
    {
        intList.push_back(i);
    }

    // iterate through list and display each item
    for (list<int>::iterator it = intList.begin(); it != intList.end(); it++)
    {
        cout << (*it) << endl;
    }
    clock_t end = clock();

    // calculate time in seconds
    double timeSeconds = ((double) end - start) / (double) CLOCKS_PER_SEC;

    // output the results
    cout.precision(10);
    cout << "C++ time to build and display list: " << timeSeconds << " seconds" << endl;
    cout << "Number of elements: " << intList.size() << endl;

    return 0;
 }


Here are the results from running this program in Microsoft Visual Studio Debug Mode, on Windows 10 running on a Dell Latitude E6420 Laptop with dual core 2.53 Ghz Intel processors and 8 GB of Ram:


Next, here are the results running the same C++ program on Unix after compiling with g++ compiler from the command line.

First our results with running the program on OpenBSD Unix at https://tilde.institute


Then our results running the program with NetBSD Unix on https://sdf.org


Next, here is the results and also how we would build the same C++ program from the command line on a Macbook Pro with macOS Catalina and using the terminal command line interface (CLI). Since Macbooks are based on BSD Unix, this can easily be done from the command line. Although C++ is also an option in the XCode development environment. It may be easier to use an advanced graphical development environment like XCode, but for this example we are using the CLI. Note the incredible speed with which the program ran, not sure why it would be so much faster than on a Dell laptop with Windows 10. Possibly because the Macbook has an SSD drive instead of a normal disk hard drive.



Next, we will test our C++ list program on Ubuntu Linux with a free shell account from https://tilde.team. Here is the results we get from that test, accessing tilde.team with the Termius ssh app and also using the g++ compiler:


Finally, here is the results running the C++ List program on Kali Linux with a Dell Latitude E6400 Laptop with dual core 2 Ghz Intel processors and 4 GB of Ram and also using the g++ compiler. Kali Linux is an excellent free open source operating system built in part from the MetaSploit utility for computer security testing.


C Programming Language

The C Programming Language was developed in the 1970's at Bell Labs by primarily computer scientist Dennis Ritchie, along with some help from others. It was revolutionary for the time and has had a huge impact on computing in general. C does not have a LIST type built-in like C++, C#, Java, Objective C, and Swift do, but its still possible to create lists called LINKED LISTS using pointer variables. In our next example program it will be generating a list with 100 thousand elements and then displaying the contents of the list to the console output. We will run it on Windows and also OpenBSD Unix and NetBSD Unix and macOS Catalina on a Macbook Pro, and also test it on Ubuntu Linux and Kali Linux, and time the results.

Here is the source code for our C list program in copy and pastable text.

 // CList.c - By Michael G. Workman
 //
 // This C console application builds a list and outputs the results of the list
 // then times the results
 //
 // This application is freely distributable under terms of MIT open source license

 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h>

This is the results of running our C List program on Microsoft Windows 10 in the Visual Studio 2019 Debug environment, on a Dell e6420 Laptop with dual core 2.53 Ghz processors and 8 GB of Ram.


Here are the results from running the same C List program on OpenBSD Unix after compiling it with the gcc compiler.


Here are the results from running the same C List program on NetBSD Unix after compiling it with the gcc compiler.


Next, here are the results from running the program from the command line terminal on a Macbook Pro running macOS Catalina. Since macOS is based on BSD Unix, we can use the command line with gcc compiler in the same way we did with OpenBSD and NetBSD and Ubuntu and Kali.


Next, we will test the C program on Ubuntu Linux with gcc compiler, running on my free shell account with https://tilde.team. And also using the Termius ssh app.


Finally, here is the results of our C List program running on Kali Linux, on a Dell Latitude e6400 Laptop with dual core 2 Ghz Intel processors and 4 GB of Ram, also with the gcc compiler.


Java Programming Language

Java is a general-purpose programming language that is class-based, object-oriented, and designed to have as few implementation dependencies as possible. It is intended to let application developers write once, run anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation [Wikipedia].

Java was first developed in the early 1990's and first released in 1996. It was created by computer scientist James Gosling, who at the time was working at Sun Microsystems. The language was based upon a C/C++ similar syntax but attempted to improve on some features of those programming languages. Java also had a feature called GARBAGE COLLECTION where software engineers would not have to manually delete and free allocated memory like in C or C++. Java programs are not compiled into executable machine code, but are instead compiled into bytecodes which are then interpreted into running machine code by the Java Run Time Engine (JRE) and also the Java Virtual Machine (JVM) is used as well. As a result of this, Java programs normally run slower than equivalent C or C++ programs, but this deficiency can be remedied by use of Java Just In Time (JIT) compilers. Also in modern times, with dual core and quad core very fast computer processors, the speed difference is very negligible.

Some notable events that happened in the Java world, in the early years of Twitter, Ruby on Rails (RoR) was used for their main web application, but turned out to be a terrible choice when RoR could not scale to the millions of users on Twitter. The solution for Twitter was to use Java instead of Ruby on Rails, which enabled Twitter to scale to millions, and then billions, of users.

Another notable event in the Java world, was the acquisition of Sun Microsystems by Oracle on January 27, 2010. Shortly after, Oracle attempted to sue Google for Google's use of a Java similar language in the Android mobile device operating system. The lawsuit was not successful, though. The Court ruled that API's like those used by Java cannot be copyrighted.

James Gosling is a Canadian computer scientist who is the creator of the Java Programming Language. He earned his PhD in computer science from Carnegie Mellon University. Gosling left Sun Microsystems not long after the acquisition by Oracle, to work at Google, and after some more job changes, now works at Amazon Web Services (AWS).






Our Java equivalent of our List program will create a list of 100 thousand consecutive integers, then display that list to the console, and then time the results. We will run the Java list program on Windows 10 from the Eclipse development environment, and also from BSD Unix on OpenBSD Unix on https://tilde.institute

Here is the source code for our Java List program in copy and pastable text:

 // JavaList.java - By Michael G. Workman
 //
 // This Java console application builds a list and then outputs the contents of the list, 
 // timing the results
 //
 // This application is freely distributable under terms of MIT open source license

 import java.util.*;
 import java.io.*;
 import java.util.concurrent.*;

 public class JavaList {

    public static void main(String[] args) {
  
     // declare list object
     List intList = new ArrayList();
  
     // loop and add 100 thousand integers to list
     // time the amount of time to load list and display list
     long startTime = System.nanoTime();
     for (Integer i = 0; i < 100000; i++)
     {
         intList.add(i);
     }
  
     // output contents of list
     Iterator it = intList.iterator();
     while(it.hasNext())
     {
         int i = (Integer) it.next();
         System.out.println(i);
     }
     long endTime = System.nanoTime();
  
     // output results
     double timeSeconds = (double) (endTime - startTime)/1_000_000_000.0;
     System.out.println("Java time to build and display list: " + timeSeconds + " seconds");
     System.out.println("Number Elements: " + intList.size());
    }
 }

Here are the results from running the Java List program on Windows 10 from the Eclipse Development Environment:


Here are the results from running the Java List program on OpenBSD Unix on https://tilde.institute, and also showing how to compile and run the Java List program from the command line with the JAVAC and JAVA commands.



Also, I recently configured an older laptop I have, a Dell Vostro 1500 Laptop, with dual core 2 Ghz Intel processors and 2 GB Ram, which I installed Fedora Linux and Eclipse Development Environment on and ran the Java List program and these are the results from that test:



Objective C Programming Language

The Objective C programming language was at first a very obscure and little known object oriented programming language that first came out in the 1980's. It uses messaging in the same manner as the Smalltalk programing language uses. It was created by Brad Cox and Tom Love in the 1980's and was eventually picked up by Steve Jobs and NeXT computer. In 2014 Apple came out with a new programming language called Swift, so now app developers can create new apps in either Objective C or the new Swift.

Steve Jobs was a long time entrepreneur and one of the original founders of Apple computer, along with Steve Wozniak. He was also the founder of NeXT Computer and Pixar. Steve Jobs and NeXT computer were early innovators with object oriented software in the 1980's. Steve Jobs single handedly brought the little known Objective C programming language out of obscurity to end up powering millions of Apple iPod, iPad, iPhone, and Mac computing devices all around the world.





We will create an Objective C program to build a list of 100 thousand consecutive integer numbers, then output that list, and time the results for comparison to other programming languages. We will use our our List Objective C program with OpenBSD Unix since Objective C is an option in the GNU GCC compiler collection, and also we will use a MacBook Pro with macOS Catalina and XCode to run our list program.

This is the source code of our Objective C List program in copy and pastable text:
 // main.m - By Michael G. Workman
 //
 // An example Objective C application to generate and output a list
 //
 // This example application freely distributable under terms of MIT open source license

 #import <Foundation/Foundation.h>
 #import <Foundation/NSObject.h>

 #define NUMBER_ELEMENTS 100000

 int main(int argc, const char* argv[])
 {
    // time interval
    typedef double NSTimeInterval;
 
    // generate list
    NSMutableArray* list = [[NSMutableArray alloc] init];

    // start timing
    NSDate *startTime = [NSDate date];

    // add 1 hundred thousand consecutive integers to the list
    for (int i = 0; i < NUMBER_ELEMENTS; i++)
    {
        [list addObject:[NSNumber numberWithInt:i]];
    }

    // output the contents of the list
    for (int i = 0; i < NUMBER_ELEMENTS; i++)
    {
        NSLog(@"%@", list[i]);
    }

    // end timing
    NSDate *endTime = [NSDate date];

    // get elapsed time
    NSTimeInterval elapsedTime = [endTime timeIntervalSinceDate:startTime];

    // get number of elements
    int numberElements = [list count];

    // output the results
    NSLog(@"Objective C time to build and display list: %f seconds", elapsedTime);
    NSLog(@"Number Elements: %i", numberElements);
    return 0;
 }

To compile and run an Objective C program on OpenBSD Unix requires the use of GNU Step and a special makefile. Objective C is available on https://tilde.institute as a compiler option.

The first step is to configure our .kshrc file in our home directory. The only lines needed are the one for the GNUSTEP_MAKEFILES and also the line ending in GNUStep.sh.


After the changes, to source the file, run the .kshrc file with the following command at the command line:

. ./.kshrc

If you are using bash instead of ksh, then you would make the changes to your .bashrc file in your home directory, and use the following command at the command line to source the file:

source ./.bashrc

The following is the makefile you would use to compile this Objective C program on OpenBSD Unix with GNU GCC compiler collection:



We then compile the Objective C List program with the following call to the makefile, using gmake, and then run the command in the same way as running a C or C++ executable on Unix.


This is our results after running the file, the time seems a little slow for a C based program executable, it may be that the timestamp that is printed on each output line is slowing the program down.


And finally, here are the results of running our Objective C List program on a Macbook Pro with macOS Catalina and XCode 11.3.



Swift Programming Language

Swift is a general-purpose, multi-paradigm, compiled programming language developed by Apple Inc. for iOS, iPadOS, macOS, watchOS, tvOS, Linux, and z/OS. Swift is designed to work with Apple's Cocoa and Cocoa Touch frameworks and the large body of existing Objective-C code written for Apple products. It is built with the open source LLVM compiler framework and has been included in Xcode since version 6, released in 2014. [Wikipedia]

Swift was designed to address many common issues with the Objective C programming language such as null pointer references, and also avoiding the "Pyramid of Doom" in software development. Also, Swift in part came about due to complaints from the app developer community that Objective C was difficult to work with. Swift programming language syntax is very similar to that of Objective C, and the Swift compiler was implemented in part with Objective C.

In this example List program, we will build a list of 100 thousand integers and output the results to the console, and time the results. This is being implemented in the Swift programming language on a Apple Macbook Pro with macOS Catalina and XCode 11.3.

Here is the code for our Swift List program in copy and pastable text:

 //  main.swift
 //  SwiftList - By Michael G. Workman
 //
 // An Example Swift application that builds and outputs a list
 //
 // This application freely distributable under terms of MIT open source license

 import Foundation
 import CoreFoundation

 // class for timing the list creation and display
 class AlgorithmTimer {

    let startTime:CFAbsoluteTime
    var endTime:CFAbsoluteTime?

    init() {
        startTime = CFAbsoluteTimeGetCurrent()
    }

    func stop() -> CFAbsoluteTime {
        endTime = CFAbsoluteTimeGetCurrent()
        return duration!
    }

    var duration:CFAbsoluteTime? {
         if let endTime = endTime {
             return endTime - startTime
         } else {
             return nil
         }
     }
 }

 // set timer and start
 var timer = AlgorithmTimer()

 // create list array of integers
 var intList = Array()

 // populate list with 100 thousand consecutive integer numbers
 for i in 1...100000
 {
     intList.append(i);
 }

 // output contents of list one element at a time
 for i in intList
 {
     print(i)
 }

 // get the amount of time it took to complete
 print("Swift time to build and display list: \(timer.stop()) seconds")
 print("Number elements: \(intList.endIndex)")


And here are the results from running our Swift List program on an Apple Macbook Pro with macOS Catalina and XCode 11.3:



Summary

Here is a summary of the results of our testing of the List programs in different programming languages and also on different operating systems. Each of our List programs built a list of 100,000 consecutive integers, then displayed that list to the console, then timed the process in seconds.

C# on Microsoft Windows 10: 25.307 seconds
C# on OpenBSD Unix with MONO: 5.886 seconds

C++ on Microsoft Windows 10: 57.384 seconds
C++ on OpenBSD Unix with g++: 0.36 seconds
C++ on NetBSD Unix with g++: 0.39 seconds
C++ on Apple macOS Catalina with g++: 0.18455 seconds
C++ on Ubuntu Linux with g++: 0.454164 seconds
C++ on Kali Linux with g++: 0.375065 seconds

C on Microsoft Windows 10: 10.62 seconds
C on OpenBSD Unix with gcc: 0.45 seconds
C on NetBSD Unix with gcc: 0.30 seconds
C on Apple macOS Catalina with gcc: 0.137377 seconds
C on Ubuntu Linux with gcc: 0.448893 seconds
C on Kali Linux with gcc: 0.350095 seconds

Java on Microsoft Windows 10 with Eclipse IDE: 1.2102463 seconds
Java on OpenBSD Unix with javac: 4.116152243 seconds

Objective C on OpenBSD Unix with GNU step and gcc: 13.612281 seconds
Objective C on Apple macOS Catalina with XCode IDE: 9.827997 seconds

Swift on Apple macOS Catalina with XCode IDE: 0.9555 seconds

As you can see here, there is some reason that C++ and C on Microsoft Windows 10 are running a good bit slower than the same programs running on BSD Unix and Apple macOS and Linux. My best insight would be because those programs on Windows 10 were containerized inside of the Microsoft Visual Studio Debug environment. Running those programs outside of Visual Studio may yield some significant performance gains. Also, from these results, the clear winner for overall speed is C and C++ on BSD Unix and Apple macOS, and also Java, but the others are not far behind. In today's world of speedy dual core and quad core processor computers the speed differences may be negligible and unimportant overall.

The source code for all of these example List programs is available in copy and pastable text at https://mworkman72.tilde.institute/list/

Also, you can see many of my software projects on my GitHub source control at https://github.com/Michael-G-Workman/

If you have any questions about software development in general, including mobile apps, web apps, relational databases, Windows, Linux, Unix, Microsoft Azure Cloud, and/or safety critical C/C++, please feel free to contact me by email at:

michael DOT g DOT workman AT gmail DOT COM.






Comments

Popular Posts