Created
May 24, 2019 08:30
-
-
Save run-dlang/9f29a83b7b6754da98993063029ef93c to your computer and use it in GitHub Desktop.
Code shared from run.dlang.io. Run with '-O3 -ffast-math'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.math; | |
import std.range; | |
import std.random; | |
import std.algorithm; | |
import std.stdio, core.stdc.stdlib; | |
__gshared double[512+1] QuarterSinTab; | |
void init(){ | |
const auto res = QuarterSinTab.length-1; | |
for(int i = 0; i < res; i++) | |
QuarterSinTab[i] = sin(PI*(i/cast(double)res)); | |
QuarterSinTab[$-1] = QuarterSinTab[0]; | |
} | |
auto sinTab(string Method = "Linear")(typeof(QuarterSinTab[0]) x) | |
{ | |
alias T = typeof(QuarterSinTab[0]); | |
auto len = QuarterSinTab.length - 1; | |
auto s = -1; | |
auto m = x - cast(int)x; | |
auto m2 = 1; | |
if (x < 0) { m = m + 1; s = 1; } | |
if ((abs(x) % 2) < 1) s = -s; | |
auto a = m*len; | |
auto ai = cast(int)(m*len); | |
auto f = a - ai; | |
return s*((1 - f)*QuarterSinTab[ai&511] + f*QuarterSinTab[(ai+1)&511]); | |
} | |
void main() { | |
init(); | |
auto x = iota(-10, 10, 0.01).map!(x => x * PI); | |
auto y = x.map!("sin(PI*a)"); | |
auto z = x.map!((x){ return sin(PI*x) - sinTab(x);}); | |
import std.datetime; | |
double xxx = 0; | |
StopWatch sw; | |
sw.start(); | |
for(double i = 0; i < 10000000; i++) xxx += sinTab(i); | |
auto t = sw.peek().msecs; | |
writeln(t); | |
sw.stop(); | |
writeln(xxx); | |
xxx = 0; | |
sw.reset(); | |
sw.start(); | |
for(double i = 0; i < 10000000; i++) xxx += sin(PI*i); | |
t = sw.peek().msecs; | |
writeln(t); | |
sw.stop(); | |
writeln(xxx); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment