<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Java-Programm doppelt so schnell wie C++-Implementierung?!]]></title><description><![CDATA[<p>Guten Tag. Ich habe einen FFT-Algorithmus in Java implementiert, und diesen dann in C++ umgeformt. Nur ist mir da anscheinend ein Fehler unterlaufen...<br />
Szenario:<br />
-Erstellen eines double-Arrays mit 10 * 48000 Einträgen.<br />
-Samplen einer Sinuswelle mit Frequenz 440 Hz und Abtastrate 48kHz<br />
-Errechnen der FFT von besagtem Array</p>
<p>Das Ergebnis für 440Hz wäre übrigens ca. 232630. Darauf kommt die Implementierung mit C++ auch (wenn auch ungenauer), allerdings braucht Java 1 Sekunde und C++ 2. Ich gehe davon aus, dass ich irgendwie einen Fehler in der Implementierung gemacht habe: Speicherplatz nicht freigegeben o.ä, was in Java automatisch passiert. Hier ist der Sourcecode:</p>
<p>main.cpp:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;complex&gt;
#include &lt;ctime&gt;

#include &quot;mathext.hpp&quot;

using namespace std;

int main(int argc, char* args[]) {

	clock_t start = clock();

	int lens = 10;
	int samplerate = 48000;
	int len = lens * samplerate;
	double* vals = new double[len];

	int freq = 440;

	for(int i = 0; i &lt; len; i++)
		vals[i] = sin(2 * PI * freq * i / samplerate);

	complex&lt;double&gt;* freqs = MathExt::fft(vals, len);

	delete [] vals;

	double c = freq * lens;

	double time = float(clock() - start) / CLOCKS_PER_SEC;
	cout &lt;&lt; abs(freqs[(int) floor(c)] * (1 - c + floor(c)) + freqs[(int) ceil(c)] * (c - floor(c))) &lt;&lt; &quot; - &quot; &lt;&lt; time &lt;&lt; &quot;s&quot; &lt;&lt; endl;
}
</code></pre>
<p>mathext.cpp</p>
<pre><code>#include &quot;mathext.hpp&quot;

complex&lt;double&gt;* MathExt::fft(double* x, int len) {

	int N = prevPow2(len);

	if (len == N)
		return internalfft(x, len);

	double* extendedX = new double[N = (N &lt;&lt; 1)];
	std::copy(x, x + len, extendedX);

	complex&lt;double&gt;* extendedC = internalfft(extendedX, N);

	complex&lt;double&gt;* c = new complex&lt;double&gt;[len];
	double indexScaleFactor = (double) N / len;
	for (int index = 0; index &lt; len; index++) {

		int extendedIndex = (int) floor(index * indexScaleFactor);

		double weight = ceil(index * indexScaleFactor)
				- (index * indexScaleFactor);
		double real = extendedC[extendedIndex].real() * weight
				+ extendedC[extendedIndex + 1].real() * (1 - weight);
		double imag = extendedC[extendedIndex].imag() * weight
				+ extendedC[extendedIndex + 1].imag() * (1 - weight);

		c[index] = complex&lt;double&gt;(real, imag);
	}

	delete [] extendedC;

	return c;
}

complex&lt;double&gt;* MathExt::internalfft(double* x, int len) {

	if (len == 1)
		return new complex&lt;double&gt;(x[0], 0);

	double* even = new double[len / 2];
	for (int k = 0; k &lt; len / 2; k++)
		even[k] = x[2 * k];
	complex&lt;double&gt;* q = internalfft(even, len / 2);

	double*	odd = even;
	for (int k = 0; k &lt; len / 2; k++)
		odd[k] = x[2 * k + 1];
	complex&lt;double&gt;* r = internalfft(odd, len / 2);

	complex&lt;double&gt;* y = new complex&lt;double&gt;[len];
	for (int k = 0; k &lt; len / 2; k++) {

		double kth = -2 * k * PI / len;
		complex&lt;double&gt; wk = complex&lt;double&gt;(cos(kth), sin(kth));
		y[k] = q[k] + (wk * r[k]);
		y[k + len / 2] = q[k] - (wk * r[k]);
	}

	delete [] q;
	delete [] r;

	return y;
}

int MathExt::prevPow2(int i) {

	i = (i &gt;&gt; 1) | i;
	i = (i &gt;&gt; 2) | i;
	i = (i &gt;&gt; 4) | i;
	i = (i &gt;&gt; 8) | i;
	i = (i &gt;&gt; 16) | i;
	return (i &gt;&gt; 1) + 1;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/322482/java-programm-doppelt-so-schnell-wie-c-implementierung</link><generator>RSS for Node</generator><lastBuildDate>Fri, 17 Jul 2026 21:37:37 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/322482.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 23 Dec 2013 03:07:22 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Java-Programm doppelt so schnell wie C++-Implementierung?! on Mon, 23 Dec 2013 03:13:38 GMT]]></title><description><![CDATA[<p>Guten Tag. Ich habe einen FFT-Algorithmus in Java implementiert, und diesen dann in C++ umgeformt. Nur ist mir da anscheinend ein Fehler unterlaufen...<br />
Szenario:<br />
-Erstellen eines double-Arrays mit 10 * 48000 Einträgen.<br />
-Samplen einer Sinuswelle mit Frequenz 440 Hz und Abtastrate 48kHz<br />
-Errechnen der FFT von besagtem Array</p>
<p>Das Ergebnis für 440Hz wäre übrigens ca. 232630. Darauf kommt die Implementierung mit C++ auch (wenn auch ungenauer), allerdings braucht Java 1 Sekunde und C++ 2. Ich gehe davon aus, dass ich irgendwie einen Fehler in der Implementierung gemacht habe: Speicherplatz nicht freigegeben o.ä, was in Java automatisch passiert. Hier ist der Sourcecode:</p>
<p>main.cpp:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;complex&gt;
#include &lt;ctime&gt;

#include &quot;mathext.hpp&quot;

using namespace std;

int main(int argc, char* args[]) {

	clock_t start = clock();

	int lens = 10;
	int samplerate = 48000;
	int len = lens * samplerate;
	double* vals = new double[len];

	int freq = 440;

	for(int i = 0; i &lt; len; i++)
		vals[i] = sin(2 * PI * freq * i / samplerate);

	complex&lt;double&gt;* freqs = MathExt::fft(vals, len);

	delete [] vals;

	double c = freq * lens;

	double time = float(clock() - start) / CLOCKS_PER_SEC;
	cout &lt;&lt; abs(freqs[(int) floor(c)] * (1 - c + floor(c)) + freqs[(int) ceil(c)] * (c - floor(c))) &lt;&lt; &quot; - &quot; &lt;&lt; time &lt;&lt; &quot;s&quot; &lt;&lt; endl;
}
</code></pre>
<p>mathext.cpp</p>
<pre><code>#include &quot;mathext.hpp&quot;

complex&lt;double&gt;* MathExt::fft(double* x, int len) {

	int N = prevPow2(len);

	if (len == N)
		return internalfft(x, len);

	double* extendedX = new double[N = (N &lt;&lt; 1)];
	std::copy(x, x + len, extendedX);

	complex&lt;double&gt;* extendedC = internalfft(extendedX, N);

	complex&lt;double&gt;* c = new complex&lt;double&gt;[len];
	double indexScaleFactor = (double) N / len;
	for (int index = 0; index &lt; len; index++) {

		int extendedIndex = (int) floor(index * indexScaleFactor);

		double weight = ceil(index * indexScaleFactor)
				- (index * indexScaleFactor);
		double real = extendedC[extendedIndex].real() * weight
				+ extendedC[extendedIndex + 1].real() * (1 - weight);
		double imag = extendedC[extendedIndex].imag() * weight
				+ extendedC[extendedIndex + 1].imag() * (1 - weight);

		c[index] = complex&lt;double&gt;(real, imag);
	}

	delete [] extendedC;

	return c;
}

complex&lt;double&gt;* MathExt::internalfft(double* x, int len) {

	if (len == 1)
		return new complex&lt;double&gt;(x[0], 0);

	double* even = new double[len / 2];
	for (int k = 0; k &lt; len / 2; k++)
		even[k] = x[2 * k];
	complex&lt;double&gt;* q = internalfft(even, len / 2);

	double*	odd = even;
	for (int k = 0; k &lt; len / 2; k++)
		odd[k] = x[2 * k + 1];
	complex&lt;double&gt;* r = internalfft(odd, len / 2);

	complex&lt;double&gt;* y = new complex&lt;double&gt;[len];
	for (int k = 0; k &lt; len / 2; k++) {

		double kth = -2 * k * PI / len;
		complex&lt;double&gt; wk = complex&lt;double&gt;(cos(kth), sin(kth));
		y[k] = q[k] + (wk * r[k]);
		y[k + len / 2] = q[k] - (wk * r[k]);
	}

	delete [] q;
	delete [] r;

	return y;
}

int MathExt::prevPow2(int i) {

	i = (i &gt;&gt; 1) | i;
	i = (i &gt;&gt; 2) | i;
	i = (i &gt;&gt; 4) | i;
	i = (i &gt;&gt; 8) | i;
	i = (i &gt;&gt; 16) | i;
	return (i &gt;&gt; 1) + 1;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2373330</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2373330</guid><dc:creator><![CDATA[Ikaron]]></dc:creator><pubDate>Mon, 23 Dec 2013 03:13:38 GMT</pubDate></item><item><title><![CDATA[Reply to Java-Programm doppelt so schnell wie C++-Implementierung?! on Mon, 23 Dec 2013 03:42:57 GMT]]></title><description><![CDATA[<p>Lass doch mal nen Profiler laufen.<br />
Im Releasemode getestet? Kann man aber eh schlecht vergleichen da wir nicht mal deinen javacode haben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2373331</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2373331</guid><dc:creator><![CDATA[Namenloser324]]></dc:creator><pubDate>Mon, 23 Dec 2013 03:42:57 GMT</pubDate></item><item><title><![CDATA[Reply to Java-Programm doppelt so schnell wie C++-Implementierung?! on Mon, 23 Dec 2013 04:59:45 GMT]]></title><description><![CDATA[<p>Bitte einmal kompilierbaren Sourcecode in einer Datei bzw. einem C/C++-Tag posten, dann kann man das sofort bei sich ausprobieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2373333</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2373333</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Mon, 23 Dec 2013 04:59:45 GMT</pubDate></item><item><title><![CDATA[Reply to Java-Programm doppelt so schnell wie C++-Implementierung?! on Mon, 23 Dec 2013 06:49:05 GMT]]></title><description><![CDATA[<p>Ikaron schrieb:</p>
<blockquote>
<p>Das Ergebnis für 440Hz wäre übrigens ca. 232630. Darauf kommt die Implementierung mit C++ auch (wenn auch ungenauer), allerdings braucht Java 1 Sekunde und C++ 2. Ich gehe davon aus, dass ich irgendwie einen Fehler in der Implementierung gemacht habe: Speicherplatz nicht freigegeben o.ä, was in Java automatisch passiert.</p>
</blockquote>
<p>Ich vermute, Du hast es korrekt implementiert. Aber daß C++ ein wenig Bauchweh bekommt, wenn Du mit new[] so enorm viele ganz winzige Arrays anlegst.<br />
edit: Habe new[] weggemacht und es nur 30% schneller geworden.</p>
<p>Versuche, eine Implementierung zu bauen, die in-place arbeitet.</p>
<p>Ich könnte mir denken, daß sowas wie die da <a href="http://www.drdobbs.com/cpp/a-simple-and-efficient-fft-implementatio/199500857" rel="nofollow">http://www.drdobbs.com/cpp/a-simple-and-efficient-fft-implementatio/199500857</a> schneller ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2373334</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2373334</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Mon, 23 Dec 2013 06:49:05 GMT</pubDate></item><item><title><![CDATA[Reply to Java-Programm doppelt so schnell wie C++-Implementierung?! on Mon, 23 Dec 2013 13:48:25 GMT]]></title><description><![CDATA[<p>Dein Programm verbringt die meiste Zeit damit, sin und cos auszurechnen. Wenn ich das mit einer Lookup-Table ersetze, ist das Programm 70% schneller:</p>
<p>Hier mal die C++-Version mit einem Lookup-Table:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;complex&gt;
#include &lt;ctime&gt;

using namespace std;

double sintable[524288+524288/4];
struct table_filler {
  table_filler() {
    for (int i=0; i&lt;524288+524288/4; ++i) {
      double kth = 2*i*M_PI/524288;
      sintable[i] = sin(kth);
    }
  }
} table_filler_;

double fastsin(int kth)
{
  return sintable[kth];
}

double fastcos(int kth)
{
  return sintable[kth + 524288/4];
}

int prevPow2(int i) {

    i = (i &gt;&gt; 1) | i;
    i = (i &gt;&gt; 2) | i;
    i = (i &gt;&gt; 4) | i;
    i = (i &gt;&gt; 8) | i;
    i = (i &gt;&gt; 16) | i;
    return (i &gt;&gt; 1) + 1;
}

complex&lt;double&gt;* internalfft(double* x, int len) {

    if (len == 1)
        return new complex&lt;double&gt;(x[0], 0);

    double* even = new double[len / 2];
    for (int k = 0; k &lt; len / 2; k++)
        even[k] = x[2 * k];
    complex&lt;double&gt;* q = internalfft(even, len / 2);

    double* odd = even;
    for (int k = 0; k &lt; len / 2; k++)
        odd[k] = x[2 * k + 1];
    complex&lt;double&gt;* r = internalfft(odd, len / 2);

    complex&lt;double&gt;* y = new complex&lt;double&gt;[len];
    for (int k = 0; k &lt; len / 2; k++) {

      //double kth = -2 * k * M_PI / len;
	int kth = (unsigned long long)k*524288/len;
        complex&lt;double&gt; wk = complex&lt;double&gt;(fastcos(kth), fastsin(kth));
        y[k] = q[k] + (wk * r[k]);
        y[k + len / 2] = q[k] - (wk * r[k]);
    }

    delete [] q;
    delete [] r;

    return y;
}

complex&lt;double&gt;* fft(double* x, int len) {

    int N = prevPow2(len);

    if (len == N)
        return internalfft(x, len);

    double* extendedX = new double[N = (N &lt;&lt; 1)];
    std::copy(x, x + len, extendedX);

    complex&lt;double&gt;* extendedC = internalfft(extendedX, N);

    complex&lt;double&gt;* c = new complex&lt;double&gt;[len];
    double indexScaleFactor = (double) N / len;
    for (int index = 0; index &lt; len; index++) {

        int extendedIndex = (int) floor(index * indexScaleFactor);

        double weight = ceil(index * indexScaleFactor)
                - (index * indexScaleFactor);
        double real = extendedC[extendedIndex].real() * weight
                + extendedC[extendedIndex + 1].real() * (1 - weight);
        double imag = extendedC[extendedIndex].imag() * weight
                + extendedC[extendedIndex + 1].imag() * (1 - weight);

        c[index] = complex&lt;double&gt;(real, imag);
    }

    delete [] extendedC;

    return c;
} 

int main() {

    clock_t start = clock();

    int lens = 10;
    int samplerate = 48000;
    int len = lens * samplerate;
    double* vals = new double[len];

    int freq = 440;

    for(int i = 0; i &lt; len; i++)
        vals[i] = sin(2 * M_PI * freq * i / samplerate);

    complex&lt;double&gt;* freqs = fft(vals, len);

    delete [] vals;

    double c = freq * lens;

    double time = float(clock() - start) / CLOCKS_PER_SEC;
    cout &lt;&lt; abs(freqs[(int) floor(c)] * (1 - c + floor(c)) + freqs[(int) ceil(c)] * (c - floor(c))) &lt;&lt; &quot; - &quot; &lt;&lt; time &lt;&lt; &quot;s&quot; &lt;&lt; endl;
}
</code></pre>
<p>(ich habe nur Zeilen 57-61 und 8-26 geändert.)</p>
<p>Ist natürlich so nicht schön, aber das gibt dir einen Anhaltspunkt, wo anzufangen ist.</p>
<p>Weiter ist das new/delete ein Problem, was uns zu einer weiteren Verbesserung führt:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;complex&gt;
#include &lt;ctime&gt;
#include &lt;vector&gt;
#include &lt;cmath&gt;
#include &lt;cassert&gt;
using namespace std;

double sintable[524288+524288/4];
struct table_filler {
  table_filler() {
    for (int i=0; i&lt;524288+524288/4; ++i) {
      double kth = 2*i*M_PI/524288;
      sintable[i] = sin(kth);
    }
  }
} table_filler_;

double fastsin(int kth)
{
  return sintable[kth];
}

double fastcos(int kth)
{
  return sintable[kth + 524288/4];
}

int prevPow2(int i) { 
  i = (i &gt;&gt; 1) | i;
  i = (i &gt;&gt; 2) | i;
  i = (i &gt;&gt; 4) | i;
  i = (i &gt;&gt; 8) | i;
  i = (i &gt;&gt; 16) | i;
  return (i &gt;&gt; 1) + 1;
}

void internalfft(double* x, int len,
		 complex&lt;double&gt; *res,
		 complex&lt;double&gt; *tmpbuf,
		 double* evenoddbuf) {

  if (len == 1) {
    *res = complex&lt;double&gt;(x[0], 0);
    return;
  }

  double* odd = evenoddbuf;
  for (int k = 0; k &lt; len / 2; k++)
    odd[k] = x[2 * k + 1];
  double *even = x;
  for (int k = 0; k &lt; len / 2; k++)
    even[k] = x[2 * k];
  std::copy(odd, odd+len/2, x+len/2);
  odd = x+len/2;

  complex&lt;double&gt;* q = tmpbuf;
  complex&lt;double&gt;* r = tmpbuf + len/2;
  internalfft(even, len / 2, q, tmpbuf + len, evenoddbuf);
  internalfft(odd, len / 2, r , tmpbuf + len, evenoddbuf + len);

  complex&lt;double&gt;* y = res;
  for (int k = 0; k &lt; len / 2; k++) {

    //double kth = -2 * k * M_PI / len;
    int kth = (unsigned long long)k*524288/len;
    complex&lt;double&gt; wk = complex&lt;double&gt;(fastcos(kth), fastsin(kth));
    y[k] = q[k] + (wk * r[k]);
    y[k + len / 2] = q[k] - (wk * r[k]);
  }
}

vector&lt;complex&lt;double&gt; &gt; fft(double* x, int len) {
  int N = prevPow2(len);
  if (len == N) {
    std::vector&lt;complex&lt;double&gt; &gt; res(len);
    std::vector&lt;complex&lt;double&gt; &gt; tmpbuf(2*len);
    std::vector&lt;double&gt; evenoddbuf(2*len);
    internalfft(x, len, res.data(), tmpbuf.data(), evenoddbuf.data());
    return res;
  }

  std::vector&lt;double&gt; extendedX((N = (N &lt;&lt; 1)));
  std::copy(x, x + len, extendedX.data());

  std::vector&lt;complex&lt;double&gt; &gt; res(N);
  std::vector&lt;complex&lt;double&gt; &gt; tmpbuf(2*N);
  std::vector&lt;double&gt; evenoddbuf(2*N);
  complex&lt;double&gt;* extendedC = res.data();
  internalfft(extendedX.data(), N, extendedC,
	      tmpbuf.data(), evenoddbuf.data());

  vector&lt;complex&lt;double&gt; &gt; c(len);
  double indexScaleFactor = (double) N / len;
  for (int index = 0; index &lt; len; index++) {

    int extendedIndex = (int) floor(index * indexScaleFactor);

    double weight = ceil(index * indexScaleFactor)
      - (index * indexScaleFactor);
    double real = extendedC[extendedIndex].real() * weight
      + extendedC[extendedIndex + 1].real() * (1 - weight);
    double imag = extendedC[extendedIndex].imag() * weight
      + extendedC[extendedIndex + 1].imag() * (1 - weight);

    c[index] = complex&lt;double&gt;(real, imag);
  }

  return c;
}

int main() {

  clock_t start = clock();

  int lens = 10;
  int samplerate = 48000;
  int len = lens * samplerate;
  std::vector&lt;double&gt; vals(len);

  int freq = 440;

  for(int i = 0; i &lt; len; i++)
    vals[i] = sin(2 * M_PI * freq * i / samplerate);

  vector&lt;complex&lt;double&gt; &gt; freqs = fft(vals.data(), len);

  double c = freq * lens;

  double time = float(clock() - start) / CLOCKS_PER_SEC;
  cout &lt;&lt; abs(freqs[(int) floor(c)] * (1 - c + floor(c)) + freqs[(int) ceil(c)] * (c - floor(c))) &lt;&lt; &quot; - &quot; &lt;&lt; time &lt;&lt; &quot;s&quot; &lt;&lt; endl;
}
</code></pre>
<p>Jetzt müsste man noch den Code etwas aufräumen (die Lookuptable komprimieren und in eine schöne Klasse kapseln, die Buffer in einen schicken Allocator verschieben etc.), dann wird der Code noch etwas schneller und vor allem schöner.</p>
<p>Aber er dürfte schon jetzt die Java-Version locker schlagen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2373386</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2373386</guid><dc:creator><![CDATA[wermisstmisstmist]]></dc:creator><pubDate>Mon, 23 Dec 2013 13:48:25 GMT</pubDate></item><item><title><![CDATA[Reply to Java-Programm doppelt so schnell wie C++-Implementierung?! on Mon, 23 Dec 2013 15:08:42 GMT]]></title><description><![CDATA[<p>wermisstmisstmist schrieb:</p>
<blockquote>
<p>Dein Programm verbringt die meiste Zeit damit, sin und cos auszurechnen. Wenn ich das mit einer Lookup-Table ersetze, ist das Programm 70% schneller:</p>
<p>Hier mal die C++-Version mit einem Lookup-Table:<br />
-snip-<br />
(ich habe nur Zeilen 57-61 und 8-26 geändert.)</p>
<p>Ist natürlich so nicht schön, aber das gibt dir einen Anhaltspunkt, wo anzufangen ist.</p>
<p>Weiter ist das new/delete ein Problem, was uns zu einer weiteren Verbesserung führt.<br />
-snip-<br />
Jetzt müsste man noch den Code etwas aufräumen (die Lookuptable komprimieren und in eine schöne Klasse kapseln, die Buffer in einen schicken Allocator verschieben etc.), dann wird der Code noch etwas schneller und vor allem schöner.</p>
<p>Aber er dürfte schon jetzt die Java-Version locker schlagen.</p>
</blockquote>
<p>Tatsächlich, die C++-Implementierung ist jetzt bei ca 0.6 Sekunden und damit ca 40% schneller als die Java-Implementierung. Allerdings ist diese auch auf 10!! Nachkommastellen genauer: 232630.3787738687 statt einfach nur 232630. Fällt jetzt zwar nicht in's Gewicht in diesem Fall, aber die Frage bleibt: Wie kann das sein? Außerdem, zu dem Lookup-table... Java benutzt den doch auch nicht, wieso ist es denn da trotzdem schneller?</p>
<p>Soo, ich hab jetzt die Lookup-table auch in Java eingebaut und siehe da: 0.381 Sekunden, also immer noch 80% schneller als C++. Aber danke schon mal für den Tipp, somit ist es wenigstens schon um Faktor 2,5 schneller geworden <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2373397</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2373397</guid><dc:creator><![CDATA[Ikaron]]></dc:creator><pubDate>Mon, 23 Dec 2013 15:08:42 GMT</pubDate></item><item><title><![CDATA[Reply to Java-Programm doppelt so schnell wie C++-Implementierung?! on Mon, 23 Dec 2013 15:08:36 GMT]]></title><description><![CDATA[<p>Ikaron schrieb:</p>
<blockquote>
<p>Allerdings ist diese auch auf 10!! Nachkommastellen genauer: 232630.3787738687 statt einfach nur 232630. Fällt jetzt zwar nicht in's Gewicht in diesem Fall, aber die Frage bleibt: Wie kann das sein?</p>
</blockquote>
<p>Der Grund ist, dass cout standardmässig bei der Ausgabe rundet.</p>
<pre><code class="language-cpp">std::cout &lt;&lt; std::setprecision(10) &lt;&lt; &lt;das Resultat&gt; &lt;&lt; '\n';
</code></pre>
<p>sollte da ausreichen.</p>
<blockquote>
<p>Außerdem, zu dem Lookup-table... Java benutzt den doch auch nicht, wieso ist es denn da trotzdem schneller?</p>
</blockquote>
<p>Ich kenn mich mit den genauen Implementierungen von sin/cos nicht aus, aber da gibt es mehrere Möglichkeiten:</p>
<ol>
<li>Javas cos/sin ist ungenauer (und dadurch schneller).</li>
<li>Javas sin/cos hat eine grössere Lookuptable (und braucht dadurch mehr Speicher).</li>
</ol>
<p>Fakt ist jedenfalls, dass die JVM cos/sin extrem optimiert hat (<a href="https://blogs.oracle.com/jag/entry/transcendental_meditation" rel="nofollow">z.B. mit argument reduction</a>), weshalb sie ev. schneller ist als die Default-Implementierung der C++-Standardbibliothek. Aber im Grund vergleicht man da Äpfel und Birnen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2373400</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2373400</guid><dc:creator><![CDATA[wermisstmisstmist]]></dc:creator><pubDate>Mon, 23 Dec 2013 15:08:36 GMT</pubDate></item><item><title><![CDATA[Reply to Java-Programm doppelt so schnell wie C++-Implementierung?! on Mon, 23 Dec 2013 15:09:27 GMT]]></title><description><![CDATA[<p>Ikaron schrieb:</p>
<blockquote>
<p>Soo, ich hab jetzt die Lookup-table auch in Java eingebaut und siehe da: 0.381 Sekunden, also immer noch 80% schneller als C++. Aber danke schon mal für den Tipp, somit ist es wenigstens schon um Faktor 2,5 schneller geworden <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
</blockquote>
<p>Wie rufst du den C++-Compiler auf?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2373401</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2373401</guid><dc:creator><![CDATA[wermisstmisstmist]]></dc:creator><pubDate>Mon, 23 Dec 2013 15:09:27 GMT</pubDate></item><item><title><![CDATA[Reply to Java-Programm doppelt so schnell wie C++-Implementierung?! on Mon, 23 Dec 2013 15:26:59 GMT]]></title><description><![CDATA[<p>wermisstmisstmist schrieb:</p>
<blockquote>
<p>Ikaron schrieb:</p>
<blockquote>
<p>Soo, ich hab jetzt die Lookup-table auch in Java eingebaut und siehe da: 0.381 Sekunden, also immer noch 80% schneller als C++. Aber danke schon mal für den Tipp, somit ist es wenigstens schon um Faktor 2,5 schneller geworden <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
</blockquote>
<p>Wie rufst du den C++-Compiler auf?</p>
</blockquote>
<p>Ehm... Ich such eben das Command raus.. Nicht wundern, ist automatisch generiert. Gibt's hier auch Tricks um nicht immer C:/Projects/speedtest/... schreiben zu müssen?</p>
<pre><code>C:\MinGW\bin\g++.exe -IC:/Projects/speedtest/src/ C:/Projects/speedtest/src/main.cpp C:/Projects/speedtest/src/mathext.cpp C:/Projects/speedtest/src/mathext.hpp -o C:/Projects/speedtest/out.exe -lmingw32
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2373403</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2373403</guid><dc:creator><![CDATA[Ikaron]]></dc:creator><pubDate>Mon, 23 Dec 2013 15:26:59 GMT</pubDate></item><item><title><![CDATA[Reply to Java-Programm doppelt so schnell wie C++-Implementierung?! on Mon, 23 Dec 2013 15:29:34 GMT]]></title><description><![CDATA[<p>Ikaron schrieb:</p>
<blockquote>
<pre><code>C:\MinGW\bin\g++.exe -IC:/Projects/speedtest/src/ C:/Projects/speedtest/src/main.cpp C:/Projects/speedtest/src/mathext.cpp C:/Projects/speedtest/src/mathext.hpp -o C:/Projects/speedtest/out.exe -lmingw32
</code></pre>
</blockquote>
<p>Na dann ist klar. Versuch in der IDE den Release-Mode einzustellen. Es sollte mindestens das Flag -O3 erscheinen, besser wäre -Ofast -march=native -flto.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2373404</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2373404</guid><dc:creator><![CDATA[wermisstmisstmist]]></dc:creator><pubDate>Mon, 23 Dec 2013 15:29:34 GMT</pubDate></item><item><title><![CDATA[Reply to Java-Programm doppelt so schnell wie C++-Implementierung?! on Mon, 23 Dec 2013 15:41:00 GMT]]></title><description><![CDATA[<p>wermisstmisstmist schrieb:</p>
<blockquote>
<p>Ikaron schrieb:</p>
<blockquote>
<pre><code>C:\MinGW\bin\g++.exe -IC:/Projects/speedtest/src/ C:/Projects/speedtest/src/main.cpp C:/Projects/speedtest/src/mathext.cpp C:/Projects/speedtest/src/mathext.hpp -o C:/Projects/speedtest/out.exe -lmingw32
</code></pre>
</blockquote>
<p>Na dann ist klar. Versuch in der IDE den Release-Mode einzustellen. Es sollte mindestens das Flag -O3 erscheinen, besser wäre -Ofast -march=native -flto.</p>
</blockquote>
<p>Die &quot;IDE&quot; ist zur Zeit nur ein kleiner Compiler-Wrapper, den ich mir selbst geschrieben habe, deswegen wäre es ganz nett, wenn du mir die Commands schnell erklären würdest. Und noch eine Frage, warum genau 524288?<br />
Tatsache. Von 600ms auf 125ms.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2373405</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2373405</guid><dc:creator><![CDATA[Ikaron]]></dc:creator><pubDate>Mon, 23 Dec 2013 15:41:00 GMT</pubDate></item><item><title><![CDATA[Reply to Java-Programm doppelt so schnell wie C++-Implementierung?! on Mon, 23 Dec 2013 15:41:41 GMT]]></title><description><![CDATA[<p>524288 ist 2^19 und war der grösste Nenner, mit dem cos aufgerufen wurde. Wie gesagt, ich habe das sehr quick'n'dirty geschrieben, der Code muss noch aufgeräumt werden.</p>
<blockquote>
<p>Tatsache. Von 600ms auf 150ms.</p>
</blockquote>
<p>Ist die Welt jetzt wieder in Ordnung?</p>
<p>Ich rufe meinen Compiler mit<br />
g++ -std=c++11 -Wall -Wextra -march=native -Ofast fast.cpp -o fast<br />
auf. Wenn das Working Directory richtig gesetzt ist, kann auf die Pfadangaben verzichtet werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2373409</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2373409</guid><dc:creator><![CDATA[wermisstmisstmist]]></dc:creator><pubDate>Mon, 23 Dec 2013 15:41:41 GMT</pubDate></item><item><title><![CDATA[Reply to Java-Programm doppelt so schnell wie C++-Implementierung?! on Mon, 23 Dec 2013 16:54:30 GMT]]></title><description><![CDATA[<p>wermisstmisstmist schrieb:</p>
<blockquote>
<p>524288 ist 2^19 und war der grösste Nenner, mit dem cos aufgerufen wurde. Wie gesagt, ich habe das sehr quick'n'dirty geschrieben, der Code muss noch aufgeräumt werden.</p>
<blockquote>
<p>Tatsache. Von 600ms auf 150ms.</p>
</blockquote>
<p>Ist die Welt jetzt wieder in Ordnung?</p>
</blockquote>
<p>Ja. Ja das ist sie. Auch, wenn ich mich frage, warum C++ mit vector schneller klar kommt als mit einem Array...</p>
<p>wermisstmisstmist schrieb:</p>
<blockquote>
<p>Ich rufe meinen Compiler mit<br />
g++ -std=c++11 -Wall -Wextra -march=native -Ofast fast.cpp -o fast<br />
auf. Wenn das Working Directory richtig gesetzt ist, kann auf die Pfadangaben verzichtet werden.</p>
</blockquote>
<p>Okay, gut, vielen Dank Dir. Jaa das mit dem Working Dir ist mir eig schon klar, aber dann kommt es zu so unsinnigen Fehlern, weil man z.B. keine \ verwenden darf oder ein / vor dem Pfad braucht oder so... Hab schon lange genug mit -L und -l gekämpft, locker 5-6 Stunden, da verlass ich mich lieber auf absolute Pfade.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2373431</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2373431</guid><dc:creator><![CDATA[Ikaron]]></dc:creator><pubDate>Mon, 23 Dec 2013 16:54:30 GMT</pubDate></item><item><title><![CDATA[Reply to Java-Programm doppelt so schnell wie C++-Implementierung?! on Mon, 23 Dec 2013 17:09:59 GMT]]></title><description><![CDATA[<p>wermisstmisstmist schrieb:</p>
<blockquote>
<p>Fakt ist jedenfalls, dass die JVM cos/sin extrem optimiert hat (<a href="https://blogs.oracle.com/jag/entry/transcendental_meditation" rel="nofollow">z.B. mit argument reduction</a>), weshalb sie ev. schneller ist als die Default-Implementierung der C++-Standardbibliothek. Aber im Grund vergleicht man da Äpfel und Birnen.</p>
</blockquote>
<p>Die JVM hat sin/cos auf Genauigkeit optimiert, und ist daher langsamer als sin/cos der CPU. Steht auch so in dem Artikel.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2373433</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2373433</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Mon, 23 Dec 2013 17:09:59 GMT</pubDate></item><item><title><![CDATA[Reply to Java-Programm doppelt so schnell wie C++-Implementierung?! on Mon, 23 Dec 2013 17:22:55 GMT]]></title><description><![CDATA[<p>Ikaron schrieb:</p>
<blockquote>
<p>Ja. Ja das ist sie. Auch, wenn ich mich frage, warum C++ mit vector schneller klar kommt als mit einem Array...</p>
</blockquote>
<p>Tut es ja nicht.<br />
Der Code mit vector verzichtet darauf bei jedem Aufruf von internalfft neue Buffer anzulegen, sondern übergibt die Buffer an internalfft als Parameter, so dass sie wiederverwendet werden können.<br />
Das kann man mit new/delete natürlich genau so machen.</p>
<p>Nur dass vector viel praktischer ist, weil man sich nicht ums delete kümmern muss, und die Freigabe dadurch automatisch Exception-safe wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2373435</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2373435</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Mon, 23 Dec 2013 17:22:55 GMT</pubDate></item><item><title><![CDATA[Reply to Java-Programm doppelt so schnell wie C++-Implementierung?! on Mon, 23 Dec 2013 17:46:04 GMT]]></title><description><![CDATA[<p>Freu dich doch, dass Java sich nicht verstecken brauch und dazu noch 1000mal angenehmer zu programmieren ist. Wenn du nicht gerade der C++-Profi schlechthin bist, ist eh Java meist schneller.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2373442</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2373442</guid><dc:creator><![CDATA[Javareichtoft]]></dc:creator><pubDate>Mon, 23 Dec 2013 17:46:04 GMT</pubDate></item><item><title><![CDATA[Reply to Java-Programm doppelt so schnell wie C++-Implementierung?! on Mon, 23 Dec 2013 17:53:47 GMT]]></title><description><![CDATA[<p>Javareichtoft schrieb:</p>
<blockquote>
<p>Wenn du nicht gerade der C++-Profi schlechthin bist, ist eh Java meist schneller.</p>
</blockquote>
<p>Man muss nicht gerade der Ober-Checker sein um beim Compiler Optimierungen anzuschalten ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2373446</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2373446</guid><dc:creator><![CDATA[javaistunnötig]]></dc:creator><pubDate>Mon, 23 Dec 2013 17:53:47 GMT</pubDate></item></channel></rss>