<?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[Code reduktion]]></title><description><![CDATA[<p>Hi,<br />
folgendes:<br />
Zeitmessung für 3 banale Summen Methoden:</p>
<p><strong>- &quot;normal&quot; e.g. int func(int n)<br />
- &quot;inline&quot;<br />
- &quot;rekursiv&quot;</strong></p>
<p>meine main sind wie folgt aus:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &quot;Testklasse.h&quot;
#include &lt;time.h&gt;
using namespace std;

int main ()
{
	Testklasse Test;

	double time1=0.0, tstart; 
	cout &lt;&lt; &quot;Anzahl Loops: &quot;;
	double anz;
	cin &gt;&gt; anz;
	tstart = clock();               
	for (int i = 0; i &lt;=anz ; i++)
	{
		Test.normMeth(2) ;
	}
	time1 += clock() - tstart;     
	time1 = time1/CLOCKS_PER_SEC;  
	cout &lt;&lt; &quot;  time normMeth= &quot; &lt;&lt; time1 &lt;&lt; &quot; sec.&quot; &lt;&lt; endl;

	time1=0.0;
	tstart = clock();
	for (int i = 0; i &lt;=anz ; i++)
	{	
		Test.rekMeth(2,2) ;
	}
	time2 += clock() - tstart;     
	time2 = time2/CLOCKS_PER_SEC;
	cout &lt;&lt; &quot;  time  rekMeth= &quot; &lt;&lt; time1 &lt;&lt; &quot; sec.&quot; &lt;&lt; endl;

	time1=0.0;	
	tstart = clock();
	for (int i = 0; i &lt;=anz ; i++)
	{
		Test.inlMeth(2) ;
	}
	time3 += clock() - tstart;     
	time3 = time3/CLOCKS_PER_SEC;
	cout &lt;&lt; &quot;  time  inlMeth= &quot; &lt;&lt; time1 &lt;&lt; &quot; sec.&quot; &lt;&lt; endl;
	cin.sync();
	cin.get();
	return 0;
}
</code></pre>
<p>Problem: wie reduziert man den Code sodass am Ende vielleicht nur eine for-schleife steht?<br />
Ist doch minimierbar oder?</p>
<p>danke für die Antworten<img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/251544/code-reduktion</link><generator>RSS for Node</generator><lastBuildDate>Tue, 15 Sep 2026 05:13:10 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/251544.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 06 Oct 2009 18:44:58 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Code reduktion on Tue, 06 Oct 2009 18:45:40 GMT]]></title><description><![CDATA[<p>Hi,<br />
folgendes:<br />
Zeitmessung für 3 banale Summen Methoden:</p>
<p><strong>- &quot;normal&quot; e.g. int func(int n)<br />
- &quot;inline&quot;<br />
- &quot;rekursiv&quot;</strong></p>
<p>meine main sind wie folgt aus:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &quot;Testklasse.h&quot;
#include &lt;time.h&gt;
using namespace std;

int main ()
{
	Testklasse Test;

	double time1=0.0, tstart; 
	cout &lt;&lt; &quot;Anzahl Loops: &quot;;
	double anz;
	cin &gt;&gt; anz;
	tstart = clock();               
	for (int i = 0; i &lt;=anz ; i++)
	{
		Test.normMeth(2) ;
	}
	time1 += clock() - tstart;     
	time1 = time1/CLOCKS_PER_SEC;  
	cout &lt;&lt; &quot;  time normMeth= &quot; &lt;&lt; time1 &lt;&lt; &quot; sec.&quot; &lt;&lt; endl;

	time1=0.0;
	tstart = clock();
	for (int i = 0; i &lt;=anz ; i++)
	{	
		Test.rekMeth(2,2) ;
	}
	time2 += clock() - tstart;     
	time2 = time2/CLOCKS_PER_SEC;
	cout &lt;&lt; &quot;  time  rekMeth= &quot; &lt;&lt; time1 &lt;&lt; &quot; sec.&quot; &lt;&lt; endl;

	time1=0.0;	
	tstart = clock();
	for (int i = 0; i &lt;=anz ; i++)
	{
		Test.inlMeth(2) ;
	}
	time3 += clock() - tstart;     
	time3 = time3/CLOCKS_PER_SEC;
	cout &lt;&lt; &quot;  time  inlMeth= &quot; &lt;&lt; time1 &lt;&lt; &quot; sec.&quot; &lt;&lt; endl;
	cin.sync();
	cin.get();
	return 0;
}
</code></pre>
<p>Problem: wie reduziert man den Code sodass am Ende vielleicht nur eine for-schleife steht?<br />
Ist doch minimierbar oder?</p>
<p>danke für die Antworten<img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1788877</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1788877</guid><dc:creator><![CDATA[pampy]]></dc:creator><pubDate>Tue, 06 Oct 2009 18:45:40 GMT</pubDate></item><item><title><![CDATA[Reply to Code reduktion on Tue, 06 Oct 2009 18:49:53 GMT]]></title><description><![CDATA[<p>Ich glaube nicht das man es in eine for-Schleifen schreiben kann da du 3 unterschiedliche routinen n-Mal ausführen willst. Wenn du also später ein unverfälschtes Ergebnis der drei Funktionen erhalten willst müssen diese auch unabhängig, sprich nicht in einer Schleife, ausgeführt werden.</p>
<p>Lg Tobi</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1788879</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1788879</guid><dc:creator><![CDATA[T0bi]]></dc:creator><pubDate>Tue, 06 Oct 2009 18:49:53 GMT</pubDate></item><item><title><![CDATA[Reply to Code reduktion on Tue, 06 Oct 2009 18:55:40 GMT]]></title><description><![CDATA[<p>Als erstes solltest du eine einheitliche Funktionsschnittstelle finden, um die drei spezifischen Funktionen dann generisch ansprechen zu können.</p>
<pre><code class="language-cpp">void TestRoutine1(TestKlasse&amp; Test)
{
   Test.normMeth(2);
}
</code></pre>
<p>Anschliessend erstellst du eine weitere Funktion, welche einen Zeiger auf die jeweilige Testroutine als Parameter erhält.</p>
<pre><code class="language-cpp">void MesseZeit(Testklasse&amp; Objekt, void (*TestRoutine)(Testklasse&amp;))
{
    time1=0.0;
    tstart = clock();
    for (int i = 0; i &lt;=anz ; i++)
    {    
        TestRoutine(Objekt);
    }
    time2 += clock() - tstart;     
    time2 = time2/CLOCKS_PER_SEC;
    cout &lt;&lt; &quot;  time  rekMeth= &quot; &lt;&lt; time1 &lt;&lt; &quot; sec.&quot; &lt;&lt; endl;
}
</code></pre>
<p>Dann rufst du diese jeweils auf:</p>
<pre><code class="language-cpp">Testklasse MeinTestObjekt;
MesseZeit(MeinTestObjekt, &amp;TestRoutine1);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1788882</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1788882</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Tue, 06 Oct 2009 18:55:40 GMT</pubDate></item><item><title><![CDATA[Reply to Code reduktion on Tue, 06 Oct 2009 23:18:46 GMT]]></title><description><![CDATA[<p>oder direkt memberpointer <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<pre><code class="language-cpp">void (testklasse::*memper)(int val) = &amp;testklasse::func1;

testklasse t;
t.*memptr(1);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1788963</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1788963</guid><dc:creator><![CDATA[member]]></dc:creator><pubDate>Tue, 06 Oct 2009 23:18:46 GMT</pubDate></item><item><title><![CDATA[Reply to Code reduktion on Sun, 11 Oct 2009 17:23:49 GMT]]></title><description><![CDATA[<p>hallo,</p>
<p>erstmal danke für die Antworten.<br />
hört sich alles sehr interessant an. <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="😃"
    /> Bin leider etwas überfordert.<br />
Testklasse.h</p>
<pre><code class="language-cpp">#pragma once

class Testklasse
{
public:
	Testklasse(void);
	~Testklasse(void);
	int normMeth(int n);
	inline int inlMeth(int n)
	{
		return n+n;
	}
	int rekMeth(int n, int times);

};
</code></pre>
<p>Testklasse.cpp</p>
<pre><code class="language-cpp">#include &quot;Testklasse.h&quot;
//using namespace std;

Testklasse::Testklasse(void)
{
}

Testklasse::~Testklasse(void)
{
}

int Testklasse::normMeth(int n)
{
	return n+n;
}

int Testklasse::rekMeth(int n, int times)
    {
      if(times==0) return 0;
	  else
      return n + rekMeth(n,times-1);
    }
</code></pre>
<p>und main.cpp ist im ersten Post zu sehen.</p>
<p>Was kommt nun wo rein? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1791038</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1791038</guid><dc:creator><![CDATA[pampy]]></dc:creator><pubDate>Sun, 11 Oct 2009 17:23:49 GMT</pubDate></item><item><title><![CDATA[Reply to Code reduktion on Sun, 11 Oct 2009 17:42:15 GMT]]></title><description><![CDATA[<p>Warum willst du das eigtl testen?<br />
Interessiert dich nur, was rauskommt?<br />
Oder willst du in deinem Projekt nur das schnellste nehmen?<br />
Oder oder oder^^</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1791042</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1791042</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Sun, 11 Oct 2009 17:42:15 GMT</pubDate></item><item><title><![CDATA[Reply to Code reduktion on Sun, 11 Oct 2009 17:59:44 GMT]]></title><description><![CDATA[<p>pampy schrieb:</p>
<blockquote>
<p>Bin leider etwas überfordert. [...] Was kommt nun wo rein? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
</blockquote>
<p>Ich habe doch in meinem Post relativ genau erklärt, was du tun musst. Wenn du etwas davon nicht verstehst, dann frage bitte noch einmal <em>konkret</em> nach. Ich möchte nämlich nicht noch einmal alles erklären, nur um dann wieder die gleiche Antwort zu erhalten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1791045</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1791045</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sun, 11 Oct 2009 17:59:44 GMT</pubDate></item><item><title><![CDATA[Reply to Code reduktion on Mon, 12 Oct 2009 09:01:17 GMT]]></title><description><![CDATA[<p>Hallo pampy,</p>
<p>meinst Du sowas</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &quot;Testklasse.h&quot;
#include &lt;ctime&gt;
#include &lt;boost/bind.hpp&gt;
using namespace std;

template&lt; typename F &gt;
void benchmark( F func, int anz, const char* txt )
{
    double tstart = clock();              
    for (int i = 0; i &lt;=anz ; i++)
    {
        func();
    }
    double time1 = clock() - tstart;    
    time1 /= CLOCKS_PER_SEC;  
    cout &lt;&lt; &quot;  time &quot; &lt;&lt; txt &lt;&lt; &quot;= &quot; &lt;&lt; time1 &lt;&lt; &quot; sec.&quot; &lt;&lt; endl;
}

int main ()
{
    Testklasse Test;

    cout &lt;&lt; &quot;Anzahl Loops: &quot;;
    int anz;
    cin &gt;&gt; anz;

    benchmark( boost::bind( &amp;Testklasse::normMeth, &amp;Test, 2 ), anz, &quot;normMeth&quot; );
    benchmark( boost::bind( &amp;Testklasse::rekMeth, &amp;Test, 2, 2 ), anz, &quot; rekMeth&quot; );
    benchmark( boost::bind( &amp;Testklasse::inlMeth, &amp;Test, 2 ), anz, &quot; inlMeth&quot; );
    cin.sync();
    cin.get();
    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1791243</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1791243</guid><dc:creator><![CDATA[Werner_logoff]]></dc:creator><pubDate>Mon, 12 Oct 2009 09:01:17 GMT</pubDate></item><item><title><![CDATA[Reply to Code reduktion on Mon, 12 Oct 2009 12:48:27 GMT]]></title><description><![CDATA[<p>Hallo Werner_logoff,<br />
exakt.<br />
Also templates.. wird noch im Semester durchgenommen.</p>
<p>danke für deine Antwort... werde es mal zuhause testen. <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/1791371</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1791371</guid><dc:creator><![CDATA[pampy]]></dc:creator><pubDate>Mon, 12 Oct 2009 12:48:27 GMT</pubDate></item><item><title><![CDATA[Reply to Code reduktion on Mon, 12 Oct 2009 13:55:00 GMT]]></title><description><![CDATA[<p>was macht cin.sync() gleich nochmal?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1791409</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1791409</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Mon, 12 Oct 2009 13:55:00 GMT</pubDate></item><item><title><![CDATA[Reply to Code reduktion on Mon, 12 Oct 2009 17:37:23 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>was macht cin.sync() gleich nochmal?</p>
</blockquote>
<p><a href="http://www.cplusplus.com/reference/iostream/istream/sync/" rel="nofollow">http://www.cplusplus.com/reference/iostream/istream/sync/</a><br />
?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1791531</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1791531</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Mon, 12 Oct 2009 17:37:23 GMT</pubDate></item><item><title><![CDATA[Reply to Code reduktion on Mon, 12 Oct 2009 20:07:05 GMT]]></title><description><![CDATA[<p>drakon schrieb:</p>
<blockquote>
<p>volkard schrieb:</p>
<blockquote>
<p>was macht cin.sync() gleich nochmal?</p>
</blockquote>
<p><a href="http://www.cplusplus.com/reference/iostream/istream/sync/" rel="nofollow">http://www.cplusplus.com/reference/iostream/istream/sync/</a><br />
?</p>
</blockquote>
<p>wo ist dann der unterschied zwischen<br />
<code>stream.ignore( stream.rdbuf()-&gt;in_avail() );</code><br />
und<br />
<code>stream.sync();</code><br />
?</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1791624</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1791624</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Mon, 12 Oct 2009 20:07:05 GMT</pubDate></item></channel></rss>