[JAVA] I made a prime number table output program in various languages

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.

C language

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 language

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

I made a prime number table output program in various languages
I made a prime number generation program in Python
I made a prime number generation program in Python 2
I made a payroll program in Python!
I made a Caesar cryptographic program in Python.
I made a command to generate a table comment in Django
I made a program that solves the spot the difference in seconds
A program that determines whether a number entered in Python is a prime number
I made a plugin to generate Markdown table from csv in Vim
I made a program to check the size of a file in Python
[CodeIQ] I tried to solve "This week's theme: Group made with number correspondence table" in Ruby (+11 languages)
[Memo] I tried a pivot table in Python
I made a random number graph with Numpy
〇✕ I made a game
I made a window for Log output with Tkinter
I made a script to put a snippet in README.md
HMAC in various languages
I made a command to markdown the table clipboard
Prime number 2 in Python
Hello World! Output list by various languages (scheduled to be updated in a timely manner)
I made a program to collect images in tweets that I liked on twitter with Python
I made a simple typing game with tkinter in Python
I made a quick feed reader using feedparser in Python
I made a Numer0n battle game in Java (I also made AI)
I made a puzzle game (like) with Tkinter in Python
I made an automated program for pasting screenshots in Excel
I implemented N-Queen in various languages and measured the speed
Output table structure in Django
I made a python text
I made a discord bot
I made a command to display a colorful calendar in the terminal
I made a program that automatically calculates the zodiac with tkinter
I made a C ++ learning site
[AOJ] Descending sort in various languages
I made a Line-bot using Python!
I made a CUI-based translation script (2)
I get a KeyError in pyclustering.xmeans
Infinite prime number generator in Python3
I made a fortune with Python.
Prime number generation program by Python
I made a CUI-based translation script
It's a prime number ... Counting prime numbers ...
Prime number enumeration in one line
I made a daemon with Python
When writing a program in Python
I made a web application in Python that converts Markdown to HTML
I made a Dir en gray face classifier using TensorFlow --⑥ Learning program
I made a Discord bot in Python that translates when it reacts
I made a CLI tool to convert images in each directory to PDF
I want to convert a table converted to PDF in Python back to CSV
I made a kind of simple image processing tool in Go language.
I tried to discriminate a 6-digit number with a number discrimination application made with python
I made a program to notify you by LINE when switches arrive
I made a script in python to convert .md files to Scrapbox format
I made a program to input what I ate and display calories and sugar
[IOS] I made a widget that displays Qiita trends in Pythonista3. [Python]
I made a mistake in fetching the hierarchy with MultiIndex of pandas
I wanted to create a program in Reverse Polish Notation in Python (determining if a string can be converted to a number)
I made a program in Python that reads CSV data of FX and creates a large amount of chart images