NodeJS is faster than D when calculating prime numbers. How?

I wrote a simple function to calculate primes in D. I thought it was pretty fast, calculating primes up to 100,000. But then I wanted to compare it with NodeJS. When I first ran the NodeJS script, I was struck by the difference and double-checked that I had not missed some calculation in some way. But the two are pretty functionally identical.

D:

import std.stdio; import std.math; import std.datetime; import std.file; import std.array; enum size_t ITERATIONS = 100_000; bool divisible(real n) { real d; for(d = 3; d < floor(n / 2); d += 2) { if(n % d == 0) { return true; } } return false; } void main() { StopWatch sw; size_t T = ITERATIONS; size_t C = 0; real n = 2; real r[ITERATIONS]; r[C] = n; sw.start(); C++; for(n = 3; n < T; n += 2) { if(!divisible(n)) { r[C] = n; C++; } } sw.stop(); double seconds = cast(double)sw.peek().usecs / 1_000_000; writeln("\n\n", C, " prime numbers calculated in ", seconds, " seconds."); File file = File("primes.txt", "w"); file.writeln("\n", C, " prime numbers calculated ", seconds, " seconds."); foreach(number; r[0..C]) { file.writeln(number); } file.writeln("\n", "end"); file.close(); } 

NodeJS:

 var fs = require('fs'); var ITERATIONS = 100000; function divisible(n) { var d; for(d = 3; d < Math.floor(n / 2); d += 2) { if(n % d == 0) { return true; } } return false; } (function() { var buffer = [ ], now = Date.now(), C = 0 n = 2 ; buffer.push(n); C++; for(n = 3; n < ITERATIONS; n += 2) { if(!divisible(n)) { buffer.push(n); C++; } } var time = Date.now() - now, seconds = time / 1000 ; console.log("\n\n", C, " prime numbers calculated. Process took ", seconds, " seconds."); buffer.push("\n" + C + " prime numbers calculated. Process took " + seconds + " seconds."); fs.writeFile("node_primes.txt", buffer.join("\n"), function(err) { if(err) throw err; console.log("Primes have been written to file."); }); })(); 

Results:

 Calculating 100,000 primes: D: 3.49126 seconds NodeJS: 0.652 seconds 

Can someone explain why this is happening?

Thanks in advance.

+5
source share
1 answer

Without the need to declare variables as real you force floating point arithmetic, where integer arithmetic can be used . Replace all real instances with int , get rid of this floor() , and your D program will run as fast as the Node.JS version:

 import std.stdio; import std.math; import std.datetime; import std.file; import std.array; enum size_t ITERATIONS = 100_000; bool divisible(int n) { int d; for(d = 3; d < n / 2; d += 2) { if(n % d == 0) { return true; } } return false; } void main() { StopWatch sw; size_t T = ITERATIONS; size_t C = 0; int n = 2; int r[ITERATIONS]; r[C] = n; sw.start(); C++; for(n = 3; n < T; n += 2) { if(!divisible(n)) { r[C] = n; C++; } } sw.stop(); double seconds = cast(double)sw.peek().usecs / 1_000_000; writeln("\n\n", C, " prime numbers calculated in ", seconds, " seconds."); File file = File("primes.txt", "w"); file.writeln("\n", C, " prime numbers calculated ", seconds, " seconds."); foreach(number; r[0..C]) { file.writeln(number); } file.writeln("\n", "end"); file.close(); } 
+7
source

Source: https://habr.com/ru/post/1205015/


All Articles