I tried to make one algorithm in various languages that I wanted to see once and compare it, this time I calculated 10,000 prime numbers in a total of 9 languages, output it, and display the time taken saw. Of these, I've used C, C ++, Java, PHP, and JavaScript in practice (although C and C ++ are far apart in practice these days), and I've never written any practice code (written by a person). I've read the source and rewrote it in another language), but I've touched Python and C # for a while in my personal learning. Ruby, Go was the first time I touched it.
If you want to see the source, please see GitHub because it will be too long if you put all the sources.
For the time being, the version and time taken for each language, and a brief impression. By the way, all the machines used are macOS High Sierra.
Apple LLVM version 10.0.0 (clang-1000.10.44.4) ~~ Total: 0.228 seconds ~~ No optimization Calculation time: 0.005386 seconds Output time: 0.005982 seconds With optimization (O3) Calculation time: 0.003645 seconds Output time: 0.005690 seconds
The language I used when I first programmed (numerical calculation class when I was a student). It's quick, but there are many restrictions and it feels a little difficult to use functionally.
int* generate_primes(int total)
{
int* primes = malloc(sizeof(int) * total);
primes[0] = 2;
int x = 1;
int j = 0;
int k = 1;
while (k < total) {
x += 2;
j = 0;
while (j < k && x % primes[j] != 0) {
if (primes[j] * primes[j] >= x) {
primes[k++] = x;
break;
}
j++;
}
}
return primes;
}
C++ Apple LLVM version 10.0.0 (clang-1000.10.44.4) ~~ Total: 0.234 seconds ~~ No optimization Calculation time: 0.006875 seconds Output time: 0.010263 seconds With optimization (O3) Calculation time: 0.003388 seconds Output time: 0.008505 seconds I used to use it when I was at the second company (I was resident at the customer and was doing image processing using OpenCV). That said, I wrote the C ++ code for the first time in a while. It's fast because it's an extension of C language. It is often said to be a complicated and difficult language, but if you have experience with C and have some knowledge of object orientation, it has more functions than C, so it feels easier to use. In the code below, new is used to secure the array area, but delete is done by the caller.
int* generate_primes(int total)
{
int* primes = new int[total];
primes[0] = 2;
int x = 1;
int k = 1;
while (k < total) {
int j = 0;
x += 2;
while (j < k && x % primes[j] != 0) {
if (primes[j] * primes[j] >= x) {
primes[k++] = x;
break;
}
j++;
}
}
return primes;
}
Java Java 1.8.0_202 ~~ Total: 0.487 seconds ~~ Calculation time: 0.007632128 seconds Output time: 0.358596283 seconds
I use it in my current company. When I use it for business, I use Eclipse, so I may not compile it on the console unless this is the case. A language used for various purposes such as the Web, smartphones, and embedded systems. Is it a drawback that it takes some time to start up the VM? 2019/7/7 postscript If I modify it so that the calculation time and the processing time are separated this time, the output time is much longer than the others. Why?
static int[] generatePrimes(int total) {
int primes[] = new int[total];
primes[0] = 2;
int k = 1;
for (int x = 3; k < total; x += 2) {
int i = 0;
while (i < k && x % primes[i] != 0) {
if (primes[i] * primes[i] >= x) {
primes[k++] = x;
break;
}
i++;
}
}
return primes;
}
Python Python 3.7.2 ~~ Total: 8.811 seconds ~~ Calculation time: 0.160643435 seconds Output time: 0.037948827 seconds I use it for a while, such as studying machine learning, and in my work I read sources written by people, but I rarely write Python myself. A rich library of machine learning, numerical calculations, statistics, etc. It was by far the slowest of the languages I tried this time. Is there a problem with writing? Also, on Mac, Python2 is often included by default, so switching to 3 was a bit annoying. 2019/7/7 postscript Even with improved logic, Python is still the slowest. Is it something like this, or is there a way to make it faster?
def generate_primes(self, total) :
primes = [0] * total
primes[0] = 2
x = 1
k = 1
while (k < total):
x += 2
j = 0;
while (j < k and x % primes[j] != 0):
if (primes[j] * primes[j] >= x):
primes[k] = x
k = k + 1
break
j = j + 1
return primes
PHP PHP 7.1.23 ~~ Total: 2.589 seconds ~~ Calculation time: 0.058230876922607 seconds Output time: 0.033617973327637 seconds I used it at the previous company. It is used almost exclusively for web development, and is often developed using frameworks (I usually use Laravel). You can write it freely, but be careful as you may get strange habits if you are not careful.
public function generate_primes($total) {
$primes = array_fill(0, $total, 0);
$primes[0] = 2;
$x = 1;
$k = 1;
while ($k < $total) {
$x += 2;
$j = 0;
while ($j < $k && $x % $primes[$j] != 0) {
if ($primes[$j] * $primes[$j] >= $x) {
$primes[$k++] = $x;
break;
}
$j++;
}
}
return $primes;
}
Ruby ruby 2.3.7 ~~ Total: 2.872 seconds ~~ Calculation time: 0.07298700 seconds Output time: 0.0931 1000 seconds I have never used it in practice or in personal study (after all). This is the first time I wrote Ruby code in my life. Ruby on Rails is a famous language made in Japan.
def generate_primes(total)
$primes = []
$primes[0] = 2
x = 1
k = 1
while (k < total)
x += 2
j = 0
while (j < k && ((x % $primes[j]) != 0))
if ($primes[j] * $primes[j] >= x)
$primes[k] = x
k += 1
break
end
j += 1
end
end
return $primes
end
C# Mono C# compiler version 5.20.1.19 ~~ Total: 0.261 seconds ~~ Calculation time: 0.005 seconds Output time: 0.092 seconds (The title is written in uppercase because the sharpness of lowercase letters cannot be displayed well in markdown.) It wasn't at all, but it was a language I hadn't used much, and I had the opportunity to touch it a little at the first company. It is often used for system development and game development on Windows, but it can be compiled on Mac using Mono. 2019/7/7 postscript I couldn't find a way to measure microseconds in C #. I would appreciate it if anyone could tell me.
int[] generate_primes(int total)
{
int[] primes = new int[1];
Array.Resize(ref primes, total);
primes[0] = 2;
int x = 1;
int j = 0;
int k = 1;
while (k < total) {
x += 2;
j = 0;
while (j < k && x % primes[j] != 0) {
if (primes[j] * primes[j] >= x) {
primes[k++] = x;
break;
}
j++;
}
}
return primes;
}
JavaScript Node.js v10.15.3 ~~ Total: 0.421 seconds ~~ Calculation time: 0.020833395004272462 seconds Output time: 0.186976930975914 seconds A language that is indispensable for front-end development. Frameworks such as Angular, React, and Vue.js are popular these days. Other than the front, you can use Node.js to move it.
function generate_primes(n) {
var primes = Array(n);
primes[0] = 2;
var x = 1;
var j = 0;
var k = 1;
while (k < n) {
x += 2;
j = 0;
while (j < k && x % primes[j] != 0) {
if (primes[j] * primes[j] >= x) {
primes[k++] = x;
break;
}
j++;
}
}
return primes;
}
go version go1.12.5 darwin/amd64 ~~ Total: 0.566 seconds ~~ Calculation time: 0.008543 seconds Output time: 0.060843 seconds I have never used it in practice or in personal study (after all). This is the first time I wrote Go code in my life. A language developed by Google. I don't know the details, but it seems to be popular these days.
func generate_primes(total int) []int {
var primes [] int
primes = append(primes, 2)
var x, j, k int = 1, 0, 1
for {
if k >= total {
break
}
x += 2
j = 0
for {
if j >= k || x % primes[j] == 0 {
break
}
if primes[j] * primes[j] > x {
k++
primes = append(primes, x)
break
}
j++
}
}
return primes
}
Looking at it in this way, even if the writing style is different depending on the language, the way of thinking is common to all languages (small average feeling). After all programming language is interesting. If I have a chance, I would like to do the same thing in a language other than the one I used this time. Candidates are Kotlin, Swift, Rust, Scala, VB.net.
2019/7/7 postscript Based on the advice given in the comment section, we improved the calculation method and separated the time of the prime number calculation part and the output part because they were the same. Thank you very much.
Recommended Posts