Port of the Python version to D

master
David Zaragoza Rodriguez 2018-01-27 15:55:24 +01:00
parent 612a67527b
commit 282c95ebb3
3 changed files with 51 additions and 1 deletions

View File

@ -1,5 +1,5 @@
# langs-performance
C++ vs. Python vs. Perl vs. PHP vs. Java vs. NodeJS vs. Go vs. Ruby vs. Rust vs. Swift performance benchmark
C++ vs. Python vs. Perl vs. PHP vs. Java vs. NodeJS vs. Go vs. Ruby vs. Rust vs. Swift vs. D performance benchmark
Blog articles:
* 2016: https://blog.famzah.net/2016/09/10/cpp-vs-python-vs-php-vs-java-vs-others-performance-benchmark-2016-q3/

45
primes.d Normal file
View File

@ -0,0 +1,45 @@
import std.algorithm;
import std.array;
import std.conv;
import std.datetime;
import std.math;
import std.process;
import std.range;
import std.stdio;
int[] get_primes7(int n){
if (n<2){
return [];
}
if (n==2){
return [2];
}
auto s = array(iota(3,n+1,2));
auto mroot = sqrt(cast(float)n);
auto half = s.length;
auto i = 0;
auto m = 3;
while (m <= mroot){
if (s[i] != 0){
int j = (m * m - 3) / 2;
s[j] = 0;
while (j< half){
s[j] = 0;
j += m;
}
}
i++;
m = 2*i + 3;
}
return [2]~array(filter!(a => a!=0)(s));
}
void main(){
auto start_time = Clock.currTime().toUnixTime();
auto period_time = to!int(environment.get("RUN_TIME"));
while(Clock.currTime().toUnixTime() - start_time < period_time){
auto res = get_primes7(10_000_000);
writeln("Found ", res.length, " prime numbers.");
}
}

5
run.sh
View File

@ -171,3 +171,8 @@ C='dotnet' ; SRC='primes.dotnet' ; run_benchmark 'C# .NET Core Linux' \
'util/build' 'util/run' "$C --version" 'cat' "$SRC"
rm -rf bin obj
cd .. || exit 1
##
C='ldc2' ; SRC='primes.d' ; run_benchmark 'D' \
"$C -O -of primes.d.out $SRC" './primes.d.out' "$C -version" 'head -n1' "$SRC"
rm -f ./primes.d.out