Comparison of the speed of programming languages ​​using the example of solving the problem of training a neural network

purpose of work


Compare the speed of programs written in different languages ​​and run on different operating systems. The results of the work are primarily interesting for solving problems associated with neural networks.

Iron and OS


For testing on Ubuntu and Windows (DELL Inspiron-7577 laptop):

RAM: 16Gb

CPU: Intel Core i7-7700HQ @ 8x 3.8GHz


fig. 1 (screenfetch command output on DELL Inspiron-7577 laptop under Ubuntu OS )

For testing on under MAC:

RAM: 8Gb

CPU: Intel Core i7 2.7GHz

We also tested on Raspberry pi 4: 

RAM: 4Gb

CPU: ARMv7 rev 3 (v7l) @ 4x 1,5Ghz


fig. 2 (screenfetch command output on raspberry pi 4 )

Testing program


To conduct tests, a program was simulated that imitated a network of 5 neurons; the goal of the program is to learn how to correctly solve the problem of finding an exclusive or with accuracy delta = 0.01. All parameters and properties of the neural network, as well as the operation and training algorithm, were taken from these 2 posts:

https://habr.com/en/post/312450/

https://habr.com/en/post/313216/

The only changes were are entered in the coefficients E (epsilon) - learning speed, α (alpha) - moment (E = 0.3, α = 0.5). When using the values ​​indicated in the article, the neural network for a long time (8 hours) could not find a solution.

By its structure, the program is a kind of OOP model in which the NeuronNet class operates with arrays of objects of the Neuron and Sinaps class. An object of the Sinaps class contains references to 2 objects of the Neuron class. For floating point calculations, the double data type is used.

Testing Algorithm: 

  1. An object of class NeuronNet is created.
  2. Passes a test training set. The results of the work of the neural network before training and after are displayed in the console and are used for comparison with the reference ones.
  3. The program enters an endless cycle where once in 100,000 results. If the error is less than delta = 0.01, the endless cycle ends. In the future I will call each such comparison an era. When the era passes, the time it took and the results of the calculation of the neural network for each value of the table excluding or is displayed in the console.
  4. After exiting the cycle, the program prints the elapsed time from 0 to the last era and ends.

The program was originally written in python3 (https://gitlab.com/di.romanov/neuron1.git), and subsequently translated into Java, Kotlin, C ++ (gcc), php7, ruby


fig. 3 (an example of the output of a program written in the Kotlin language running under the Ubuntu OS)

Test results


When working, programs written in Kotlin, Java, php, ruby, and Python gave the same answers after the training set; the output after the training set of a program written in C ++ was different, which entailed a change in the number of epochs that it required for proper training. For this reason, both comparisons of the running time of the entire program and the time it took to go through one era will be given.

Training Time [ms.]
UbuntuWindowsRaspbianMAC
Python104569204239521112335621
Kotlin49684877199637775
Java48925994179737652
Ruby7968490524457229
C ++100990212000505377
php75591131170513996

tab. 1 (time of passage of all eras before training the neural network) 

Travel time of one era [ms.]
UbuntuWindowsRaspbianMAC
Python8713169424331527576
Kotlin3924051631625
Java3954851434635
Ruby6667756638040
C ++4185883421057
php63811001243168

tab. 2 (time passing one era) 

Results Analysis



graph. 1 (the time of passage of all eras for programs running on the Ubuntu OS)

As expected, Kotlin and Java showed the same speed of work ahead of Python by about 20 times. Let's consider some not so obvious observations.

The results of a program written in C ++ were unexpectedly slow. In part, this can be explained by the large number of eras that it took her to find the right answer. However, even with this in mind ( see graph. 2 ), it lags behind Java programs in speed. 


graph. 2 (passage time of one era for programs running on the Ubuntu OS)

Another reason for such results may be different use of processor resources ( see Fig. 4, Fig. 5 )


. 4 ( Ubuntu Kotlin)




fig. 5 (output of the Ubuntu port monitor during execution of a program written in C ++)

As you can see, Java uses at least 4 cores at a time, while a C ++ program uses one. However, this cannot explain the speed superiority of 8 times, since Java does not use all the cores to 100%.

Significant differences in the speed of a program written in Python depending on the OS. When running Java programs on different operating systems, differences in speed were no more than 40% (even on different machines, with the exception of raspberry), but when starting the Python program, the following values ​​were obtained: Ubuntu - 104c, Windows - 204c, MAC - 335c . The ratio of the speed of the program on Kotlin to the speed of the program in Python is 21 for the Ubuntu OS, 26 for Raspberry and already 43 for the Mac.

All interpreted programming languages ​​showed the same speed on Raspbery

Python3 Translation Authors


Ruby, php

https://vk.com/silverlumen

Java

https://vk.com/id365699827

All Articles