<div dir="ltr">or for ldc <a href="http://docs.algorithm.dlang.io/latest/mir_math_common.html">http://docs.algorithm.dlang.io/latest/mir_math_common.html</a></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Apr 7, 2018 at 9:10 PM, Daniel Kozak <span dir="ltr"><<a href="mailto:kozzi11@gmail.com" target="_blank">kozzi11@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">can you try it with c math functions?<div><br></div><div>instead of std.math, try to use core.stdc.math</div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Apr 7, 2018 at 8:53 PM, Arun Chandrasekaran via Digitalmars-d-learn <span dir="ltr"><<a href="mailto:digitalmars-d-learn@puremagic.com" target="_blank">digitalmars-d-learn@<wbr>puremagic.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">What am I doing wrong here that makes the D equivalent 2.5 times slower than it's C equivalent?<br>
<br>
Compilers used:<br>
<br>
LDC2: LDC - the LLVM D compiler (1.8.0)<br>
GCC: gcc (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609<br>
<br>
11:36:39 ~/code/c/test2$ ldc2 sigmoid.d -O5 && ./sigmoid<br>
Max deviation is 0.001664<br>
10^7 iterations using sigmoid1: 308 ms<br>
10^7 iterations using sigmoid2: 30 ms<br>
11:36:55 ~/code/c/test2<br>
$ gcc sigmoid.c -o sigmoid-c -O3 -lm 2>/dev/null && ./sigmoid-c<br>
Max deviation is 0.001664<br>
10^7 iterations using sigmoid1: 134 ms<br>
10^7 iterations using sigmoid2: 29 ms<br>
11:37:10 ~/code/c/test2<br>
$<br>
<br>
C code, taken from <a href="https://stackoverflow.com/questions/412019/math-optimization-in-c-sharp#412176" rel="noreferrer" target="_blank">https://stackoverflow.com/ques<wbr>tions/412019/math-optimization<wbr>-in-c-sharp#412176</a>:<br>
<br>
```<br>
#include <math.h><br>
#include <stdio.h><br>
#include <time.h><br>
<br>
#define SCALE 320.0f<br>
#define RESOLUTION 2047<br>
#define MIN -RESOLUTION / SCALE<br>
#define MAX RESOLUTION / SCALE<br>
<br>
static float sigmoid_lut[RESOLUTION + 1];<br>
<br>
void init_sigmoid_lut(void) {<br>
int i;<br>
for (i = 0; i < RESOLUTION + 1; i++) {<br>
sigmoid_lut[i] = (1.0 / (1.0 + exp(-i / SCALE)));<br>
}<br>
}<br>
<br>
static float sigmoid1(const float value) {<br>
return (1.0f / (1.0f + expf(-value)));<br>
}<br>
<br>
static float sigmoid2(const float value) {<br>
if (value <= MIN) return 0.0f;<br>
if (value >= MAX) return 1.0f;<br>
if (value >= 0) return sigmoid_lut[(int)(value * SCALE + 0.5f)];<br>
return 1.0f-sigmoid_lut[(int)(-value * SCALE + 0.5f)];<br>
}<br>
<br>
float test_error() {<br>
float x;<br>
float emax = 0.0;<br>
<br>
for (x = -10.0f; x < 10.0f; x+=0.00001f) {<br>
float v0 = sigmoid1(x);<br>
float v1 = sigmoid2(x);<br>
float error = fabsf(v1 - v0);<br>
if (error > emax) { emax = error; }<br>
}<br>
return emax;<br>
}<br>
<br>
int sigmoid1_perf() {<br>
clock_t t0, t1;<br>
int i;<br>
float x, y = 0.0f;<br>
<br>
t0 = clock();<br>
for (i = 0; i < 10; i++) {<br>
for (x = -5.0f; x <= 5.0f; x+=0.00001f) {<br>
y = sigmoid1(x);<br>
}<br>
}<br>
t1 = clock();<br>
printf("", y); /* To avoid sigmoidX() calls being optimized away */<br>
return (t1 - t0) / (CLOCKS_PER_SEC / 1000);<br>
}<br>
<br>
int sigmoid2_perf() {<br>
clock_t t0, t1;<br>
int i;<br>
float x, y = 0.0f;<br>
t0 = clock();<br>
for (i = 0; i < 10; i++) {<br>
for (x = -5.0f; x <= 5.0f; x+=0.00001f) {<br>
y = sigmoid2(x);<br>
}<br>
}<br>
t1 = clock();<br>
printf("", y); /* To avoid sigmoidX() calls being optimized away */<br>
return (t1 - t0) / (CLOCKS_PER_SEC / 1000);<br>
}<br>
<br>
int main(void) {<br>
init_sigmoid_lut();<br>
printf("Max deviation is %0.6f\n", test_error());<br>
printf("10^7 iterations using sigmoid1: %d ms\n", sigmoid1_perf());<br>
printf("10^7 iterations using sigmoid2: %d ms\n", sigmoid2_perf());<br>
<br>
return 0;<br>
}<br>
```<br>
<br>
D equivalent:<br>
<br>
```<br>
module sigmoid;<br>
<br>
import std.stdio;<br>
import std.math;<br>
import std.datetime.stopwatch;<br>
<br>
enum SCALE = 320.0f;<br>
enum RESOLUTION = 2047;<br>
enum MIN = -RESOLUTION / SCALE;<br>
enum MAX = RESOLUTION / SCALE;<br>
<br>
float[RESOLUTION + 1] sigmoid_lut;<br>
<br>
void init_sigmoid_lut() {<br>
int i;<br>
for (i = 0; i < RESOLUTION + 1; i++) {<br>
sigmoid_lut[i] = (1.0 / (1.0 + exp(-i / SCALE)));<br>
}<br>
}<br>
<br>
private float sigmoid1(const float value) {<br>
return (1.0f / (1.0f + exp(-value)));<br>
}<br>
<br>
private float sigmoid2(const float value) {<br>
if (value <= MIN) return 0.0f;<br>
if (value >= MAX) return 1.0f;<br>
if (value >= 0) return sigmoid_lut[cast(int)(value * SCALE + 0.5f)];<br>
return 1.0f-sigmoid_lut[cast(int)(-va<wbr>lue * SCALE + 0.5f)];<br>
}<br>
<br>
private float test_error() {<br>
float x;<br>
float emax = 0.0;<br>
<br>
for (x = -10.0f; x < 10.0f; x+=0.00001f) {<br>
float v0 = sigmoid1(x);<br>
float v1 = sigmoid2(x);<br>
float error = fabs(v1 - v0);<br>
if (error > emax) { emax = error; }<br>
}<br>
return emax;<br>
}<br>
<br>
private auto sigmoid1_perf() {<br>
auto sw = StopWatch(AutoStart.yes);<br>
int i;<br>
float x, y = 0.0f;<br>
<br>
for (i = 0; i < 10; i++) {<br>
for (x = -5.0f; x <= 5.0f; x+=0.00001f) {<br>
y = sigmoid1(x);<br>
}<br>
}<br>
return sw.peek.total!"msecs";<br>
}<br>
<br>
private auto sigmoid2_perf() {<br>
auto sw = StopWatch(AutoStart.yes);<br>
int i;<br>
float x, y = 0.0f;<br>
for (i = 0; i < 10; i++) {<br>
for (x = -5.0f; x <= 5.0f; x+=0.00001f) {<br>
y = sigmoid2(x);<br>
}<br>
}<br>
return sw.peek.total!"msecs";<br>
}<br>
<br>
int main() {<br>
init_sigmoid_lut();<br>
writefln("Max deviation is %0.6f", test_error());<br>
writefln("10^7 iterations using sigmoid1: %s ms", sigmoid1_perf());<br>
writefln("10^7 iterations using sigmoid2: %s ms", sigmoid2_perf());<br>
<br>
return 0;<br>
}<br>
```<br>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>