• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Kambiz

Unity
my c++ d c# benchmark!

71 posts in this topic

Today I saw the D's pi computing sample (\dmd\samples\d\pi.d) and thought that porting it to c++ and c# for making a little benchmark should be easy: d:
import std.c.stdio;
import std.c.stdlib;
import std.c.time;

const int LONG_TIME=4000;

byte[] p;
byte[] t;
int q;

int main(char[][] args)
{
	int startime, endtime;
	int i;

	if (args.length == 2) {
		sscanf(&args[1][0],"%d",&q);
	} else {
		printf("Usage: pi [precision]\n");
		exit(55);
	}

	if (q < 0)
	{
		printf("Precision was too low, running with precision of 0.\n");
		q = 0;
	}

	if (q > LONG_TIME)
	{
	    printf("Be prepared to wait a while...\n");
	}

	// Compute one more digit than we display to compensate for rounding
	q++;

	p.length = q + 1;
	t.length = q + 1;

	/* compute pi */

	std.c.time.time(&startime);
	arctan(2);
	arctan(3);
	mul4();
	std.c.time.time(&endtime);

	// Return to the number of digits we want to display
	q--;

	/* print pi */

	printf("pi = %d.",cast(int)(p[0]));
	for (i = 1; i <= q; i++)
	printf("%d",cast(int)(p[i]));
	printf("\n");
	printf("%ld seconds to compute pi with a precision of %d digits.\n",endtime-startime,q);

	return 0;
}

void arctan(int s)
{
	int n;

	t[0] = 1;
	div(s); /* t[] = 1/s */
	add();
	n = 1;
	do {
		mul(n);
		div(s * s);
		div(n += 2);
		if (((n-1) / 2) % 2 == 0)
			add();
		else
			sub();
	} while (!tiszero());
}

void add()
{
	int j;

	for (j = q; j >= 0; j--)
	{
		if (t[j] + p[j] > 9) {
			p[j] += t[j] - 10;
			p[j-1] += 1;
		} else
			p[j] += t[j];
	}
}

void sub()
{
	int j;

	for (j = q; j >= 0; j--)
		if (p[j] < t[j]) {
			p[j] -= t[j] - 10;
			p[j-1] -= 1;
		} else
			p[j] -= t[j];
}

void mul(int multiplier)
{
	int b;
	int i;
	int carry = 0, digit = 0;

	for (i = q; i >= 0; i--) {
		b = (t[i] * multiplier + carry);
		digit = b % 10;
		carry = b / 10;
		t[i] = digit;
	}
}

/* t[] /= l */

void div(int divisor)
{
	int i, b;
	int quotient, remainder = 0;

	for (i = 0; i <= q; i++) {
		b = (10 * remainder + t[i]);
		quotient = b / divisor;
		remainder = b % divisor;
		t[i] = quotient;
	}
}

void div4()
{
	int i, c, d = 0;

	for (i = 0; i <= q; i++) {
		c = (10 * d + p[i]) / 4;
		d = (10 * d + p[i]) % 4;
		p[i] = c;
	}
}

void mul4()
{
	int i, c, d;

	d = c = 0;

	for (i = q; i >= 0; i--) {
		d = (p[i] * 4 + c) % 10;
		c = (p[i] * 4 + c) / 10;
		p[i] = d;
	}
}

int tiszero()
{
	int k;

	for (k = 0; k <= q; k++)
		if (t[k] != 0)
			return false;
	return true;
}

(I compiled pi.d with dmd -O -release pi.d) c++:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define LONG_TIME 4000

__int8* p;
__int8* t;
int q;


void arctan(int s);
void add();
void sub();
void mul(int);
void div(int);
void mul4();
void div4();
int tiszero();

int main(int argv,char** args)
{
	time_t startime, endtime;
	int i;

	if (argv == 2) {
		sscanf(&args[1][0],"%d",&q);
	} else {
		printf("Usage: pi [precision]\n");
		exit(55);
	}

	if (q < 0)
	{
		printf("Precision was too low, running with precision of 0.\n");
		q = 0;
	}

	if (q > LONG_TIME)
	{
	    printf("Be prepared to wait a while...\n");
	}

	// Compute one more digit than we display to compensate for rounding
	q++;

	p = new __int8[ q + 1 ];
	t = new __int8[ q + 1 ];
	for(int tt=0;tt<=q;tt++) 
	{
		p[tt]=0;
		t[tt]=0;
	}
	/* compute pi */

	time(&startime);
	arctan(2);
	arctan(3);
	mul4();
	time(&endtime);

	// Return to the number of digits we want to display
	q--;

	/* print pi */

	printf("pi = %d.",(int)(p[0]));
	for (i = 1; i <= q; i++)
	printf("%d",(int)(p[i]));
	printf("\n");
	printf("%ld seconds to compute pi with a precision of %d digits.\n",(long)endtime-(long)startime,q);

	delete [] p;
	delete [] t;
	return 0;
}

void arctan(int s)
{
	int n;

	t[0] = 1;
	div(s); /* t[] = 1/s */
	add();
	n = 1;
	do {
		mul(n);
		div(s * s);
		div(n += 2);
		if (((n-1) / 2) % 2 == 0)
			add();
		else
			sub();
	} while (!tiszero());
}

void add()
{
	int j;

	for (j = q; j >= 0; j--)
	{
		if (t[j] + p[j] > 9) {
			p[j] += t[j] - 10;
			p[j-1] += 1;
		} else
			p[j] += t[j];
	}
}

void sub()
{
	int j;

	for (j = q; j >= 0; j--)
		if (p[j] < t[j]) {
			p[j] -= t[j] - 10;
			p[j-1] -= 1;
		} else
			p[j] -= t[j];
}

void mul(int multiplier)
{
	int b;
	int i;
	int carry = 0, digit = 0;

	for (i = q; i >= 0; i--) {
		b = (t[i] * multiplier + carry);
		digit = b % 10;
		carry = b / 10;
		t[i] = digit;
	}
}

/* t[] /= l */

void div(int divisor)
{
	int i, b;
	int quotient, remainder = 0;

	for (i = 0; i <= q; i++) {
		b = (10 * remainder + t[i]);
		quotient = b / divisor;
		remainder = b % divisor;
		t[i] = quotient;
	}
}

void div4()
{
	int i, c, d = 0;

	for (i = 0; i <= q; i++) {
		c = (10 * d + p[i]) / 4;
		d = (10 * d + p[i]) % 4;
		p[i] = c;
	}
}

void mul4()
{
	int i, c, d;

	d = c = 0;

	for (i = q; i >= 0; i--) {
		d = (p[i] * 4 + c) % 10;
		c = (p[i] * 4 + c) / 10;
		p[i] = d;
	}
}

int tiszero()
{
	int k;

	for (k = 0; k <= q; k++)
		if (t[k] != 0)
			return false;
	return true;
}

c#:
using System;
using System.Diagnostics;

class Pi
{
    static void Main(string[] args)
    {
        Pi pi = new Pi();
        pi.run(args);
    }
    private const int LONG_TIME = 4000;
    sbyte[] p;
    sbyte[] t;
    int q;

    void run(string[] args)
    {
        Stopwatch timer = new Stopwatch();
        int i;

        if (args.Length == 1)
        {
            q = int.Parse(args[0]);
        }
        else
        {
            Console.WriteLine("Usage: pi [precision]");
            return;
        }
        
        if (q < 0)
        {
            Console.WriteLine("Precision was too low, running with precision of 0.");
            q = 0;
        }

        if (q > LONG_TIME)
        {
            Console.WriteLine("Be prepared to wait a while...");
        }

        // Compute one more digit than we display to compensate for rounding
        q++;

        p = new sbyte[q + 1];
        t = new sbyte[q + 1];
        /* compute pi */

        timer.Start();
        arctan(2);
        arctan(3);
        mul4();
        timer.Stop();

        // Return to the number of digits we want to display
        q--;

        /* print pi */

        Console.Write("pi = {0}.", p[0]);
        for (i = 1; i <= q; i++)
            Console.Write(p[i]);
        Console.WriteLine();
        Console.WriteLine("{0} seconds to compute pi with a precision of {1} digits.", timer.ElapsedMilliseconds / 1000.0, q);

        return;
    }

    void arctan(int s)
    {
        int n;

        t[0] = 1;
        div(s); /* t[] = 1/s */
        add();
        n = 1;
        do
        {
            mul(n);
            div(s * s);
            div(n += 2);
            if (((n - 1) / 2) % 2 == 0)
                add();
            else
                sub();
        } while (!tiszero());
    }

    void add()
    {
        int j;

        for (j = q; j >= 0; j--)
        {
            if (t[j] + p[j] > 9)
            {
                p[j] += (sbyte)(t[j] - 10);
                p[j - 1] += 1;
            }
            else
                p[j] += t[j];
        }
    }

    void sub()
    {
        int j;

        for (j = q; j >= 0; j--)
            if (p[j] < t[j])
            {
                p[j] -= (sbyte)(t[j] - 10);
                p[j - 1] -= 1;
            }
            else
                p[j] -= t[j];
    }

    void mul(int multiplier)
    {
        int b;
        int i;
        int carry = 0, digit = 0;

        for (i = q; i >= 0; i--)
        {
            b = (t[i] * multiplier + carry);
            digit = b % 10;
            carry = b / 10;
            t[i] = (sbyte)digit;
        }
    }

    /* t[] /= l */

    void div(int divisor)
    {
        int i, b;
        int quotient, remainder = 0;

        for (i = 0; i <= q; i++)
        {
            b = (10 * remainder + t[i]);
            quotient = b / divisor;
            remainder = b % divisor;
            t[i] = (sbyte)quotient;
        }
    }

    void div4()
    {
        int i, c, d = 0;

        for (i = 0; i <= q; i++)
        {
            c = (10 * d + p[i]) / 4;
            d = (10 * d + p[i]) % 4;
            p[i] = (sbyte)c;
        }
    }

    void mul4()
    {
        int i, c, d;

        d = c = 0;

        for (i = q; i >= 0; i--)
        {
            d = (p[i] * 4 + c) % 10;
            c = (p[i] * 4 + c) / 10;
            p[i] = (sbyte)d;
        }
    }

    bool tiszero()
    {
        int k;

        for (k = 0; k <= q; k++)
            if (t[k] != 0)
                return false;
        return true;
    }

 
}

I used the release builds for the benchmark, here are the results: d : 26 seconds to compute pi with a precision of 10000 digits. c# : 34,745(34.745) seconds to compute pi with a precision of 10000 digits. c++ : 15 seconds to compute pi with a precision of 10000 digits. I do not want to start a language war... we had enough such wars in the past days and I have just made this benchmark because I saw the “What do you think of the D language?“ thread and was testing d. I'm just surprised a little: I thought that D shouldn't be much slower than c++ and I thought that c# would be much faster. Maybe there is some optimization option I have not used(?) What do you think about the results? (maybe some one can test java) (I really like the invariant and unittest features of D, are there equivalents for c#?) -I can't reply until tomorrow- [Edited by - Kambiz on October 7, 2006 2:02:45 AM]
0

Share this post


Link to post
Share on other sites
34,745 seconds! You realy ran the program for 9 hours?
0

Share this post


Link to post
Share on other sites
In most/all of Europe, actually. So, 3.14159 would be 3,14159. You really should know that, since chances are it will bite you one time (as you saw, it made a vast difference here).

What compiler where you using for C++ and what optimizations did you enable?
0

Share this post


Link to post
Share on other sites
One of traditions in the computer technology was use of "." for 0.030. The "," was used for something else.

"," was used traditionally only in math classes. However teachers would survive "." instead if they weren't completely retarded.

BTW if you'd like to test Java, should it be a direct port, or should it use BigDecimal instead? You might like to add an 64 bit ASM program as well.
0

Share this post


Link to post
Share on other sites
Alright, managed code haters -- don't feel too vindicated yet. I sat down with the C# code for about 15 minutes, and made some modifications to the mul and div functions.
using System;
using System.Diagnostics;

class Pi
{
static void Main( string[] args )
{
Pi pi = new Pi();
pi.run( args );
}

private const int LONG_TIME = 4000;
sbyte[] p;
sbyte[] t;
int q;

void run( string[] args )
{
Stopwatch timer = new Stopwatch();
int i;

if( args.Length == 1 )
{
q = int.Parse( args[0] );
}
else
{
Console.WriteLine( "Usage: pi [precision]" );
return;
}

if( q < 0 )
{
Console.WriteLine( "Precision was too low, running with precision of 0." );
q = 0;
}

if( q > LONG_TIME )
{
Console.WriteLine( "Be prepared to wait a while..." );
}

// Compute one more digit than we display to compensate for rounding
q++;

p = new sbyte[q + 1];
t = new sbyte[q + 1];
/* compute pi */

timer.Start();
arctan( 2 );
arctan( 3 );
mul4();
timer.Stop();

// Return to the number of digits we want to display
q--;

/* print pi */

Console.Write( "pi = {0}.", p[0] );
for( i = 1; i <= q; i++ )
Console.Write( p[i] );
Console.WriteLine();
Console.WriteLine( "{0} seconds to compute pi with a precision of {1} digits.", timer.ElapsedMilliseconds / 1000.0, q );

return;
}

void arctan( int s )
{
int n;

t[0] = 1;
div( s ); /* t[] = 1/s */
add();
n = 1;
do
{
mul( n );
div( s * s );
div( n += 2 );
if( ( ( n - 1 ) / 2 ) % 2 == 0 )
add();
else
sub();
} while( !tiszero() );
}

void add()
{
int j;

for( j = q; j >= 0; j-- )
{
if( t[j] + p[j] > 9 )
{
p[j] += (sbyte) ( t[j] - 10 );
p[j - 1] += 1;
}
else
p[j] += t[j];
}
}

void sub()
{
int j;

for( j = q; j >= 0; j-- )
if( p[j] < t[j] )
{
p[j] -= (sbyte) ( t[j] - 10 );
p[j - 1] -= 1;
}
else
p[j] -= t[j];
}

void mul( int multiplier )
{
int b;
int i;
int carry = 0, digit;

for( i = q; i >= 0; i-- )
{
b = ( t[i] * multiplier + carry );
carry = (int) ( b * 0.1f );
digit = b - 10 * carry;
t[i] = (sbyte) digit;
}
}

/* t[] /= l */

void div( int divisor )
{
int i, b;
int quotient, remainder = 0;
float fdiv = 1.0f / divisor;

for( i = 0; i <= q; i++ )
{
b = ( 10 * remainder + t[i] );
quotient = (int) ( b * fdiv );
remainder = b - divisor * quotient;
t[i] = (sbyte) quotient;
}
}

void div4()
{
int i, c, d = 0;

for( i = 0; i <= q; i++ )
{
c = ( 10 * d + p[i] ) / 4;
d = ( 10 * d + p[i] ) % 4;
p[i] = (sbyte) c;
}
}

void mul4()
{
int i, c, d;

d = c = 0;

for( i = q; i >= 0; i-- )
{
d = ( p[i] * 4 + c ) % 10;
c = ( p[i] * 4 + c ) / 10;
p[i] = (sbyte) d;
}
}

bool tiszero()
{
int k;

for( k = 0; k <= q; k++ )
if( t[k] != 0 )
return false;
return true;
}
}







Initial figures on my system before modification were in the vicinity of about 19s (probably closer to 20) for the C++ and 38s for the C#. This version of the C# runs in just under 22s. I brought the same optimizations back to the C++ version, but they didn't appear to have any particular effect.

Perhaps someone can step up and defend D's honor. 26s is much worse than I expected. (Although the fact that the D code was written by Digital Mars does not bode well.)

[Edited by - Promit on October 6, 2006 9:28:51 PM]
0

Share this post


Link to post
Share on other sites
The fastest way to calculate PI in C is of course:


#define _ -F<00||--F-OO--;
int F=00,OO=00;main(){F_OO();printf("%1.3f\n",4.*-F/OO/OO);}F_OO()
{
_-_-_-_
_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_
_-_-_-_
}

0

Share this post


Link to post
Share on other sites
Quote:
Original post by TrueTom
The comma is the new dot. (At least in germany.)

yes... (I have a German Windows.)

Quote:
Original post by Promit
Alright, managed code haters -- don't feel too vindicated yet. I sat down with the C# code for about 15 minutes, and made some modifications to the mul and div functions.
...

19.443 seconds to compute pi with a precision of 10000 digits.
But because of the modifications we should not compare these result with the others.

Quote:
Original post by Ezbez
What compiler where you using for C++ and what optimizations did you enable?

MSVC++ 2005
/O2 /Ot /GL /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /FD /EHsc /MD /Fo"Release\\" /Fd"Release\vc80.pdb" /W3 /nologo /c /Wp64 /Zi /TP /errorReport:prompt
Linker:
/OUT:"D:\kambiz\c++\pi\Release\pi.exe" /INCREMENTAL:NO /NOLOGO /MANIFEST /MANIFESTFILE:"Release\pi.exe.intermediate.manifest" /DEBUG /PDB:"d:\kambiz\c++\pi\Release\pi.pdb" /SUBSYSTEM:CONSOLE /OPT:REF /OPT:ICF /LTCG /MACHINE:X86 /ERRORREPORT:PROMPT kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Kambiz
But because of the modifications we should not compare these result with the others.


Under which conditions may we compare one result with another?

0

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Quote:
Original post by Kambiz
But because of the modifications we should not compare these result with the others.


Under which conditions may we compare one result with another?
Presumably, one is only allowed to use the exact implementation originally written in C, without taking into account the fact that a good C implementation may be a terrible implementation for another language.

In short, when comparing the performance of languages, one must assume that all programmers are competent at no languages other than C.
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Promit
Presumably, one is only allowed to use the exact implementation originally written in C, without taking into account the fact that a good C implementation may be a terrible implementation for another language.


You seem to be right. I've only just noticed that the version advertised as C++ isn't C++, but is in fact C.
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Kambiz
I'm just surprised a little: I thought that D shouldn't be much slower than c++ and I thought that c# would be much faster. Maybe there is some optimization option I have not used(?)
What do you think about the results?
Interesting, but you're comparing the Microsoft C++ optimizer with the Digital Mars C++ optimizer. DMD, the Digital Mars D compiler, is built using the Digital Mars C++ optimizer.

To compare apples-apples, it makes sense to compare DMD versus DMC++, or GDC versus g++, as then the optimizer and code generator would be the same for each language. I tried DMD and DMC++, and got essentially the same times for each.



0

Share this post


Link to post
Share on other sites
I actually have a suspicion that some of the modifications I applied in the C# version will improve the D version's performance as well. Anyone want to try my edits on D and see what happens? I really don't feel like downloading DMD.
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Kambiz
d : 26 seconds to compute pi with a precision of 10000 digits.
c# : 34,745(34.745) seconds to compute pi with a precision of 10000 digits.
c++ : 15 seconds to compute pi with a precision of 10000 digits.
...
I do not want to start a language war...

Yeah, right. The fact that you only gave a decimal point only for the C# version (using a comma to represent it, making it look like a huge number) and your dismissal of Promit's benchmark implies that you hold a certain prejudice toward C#. Now, theres nothing inherently wrong with that but trying to spread propaganda in the form of a benchmark is pretty sad if you ask me.
0

Share this post


Link to post
Share on other sites
I'll make it fair barakus.

d : 26 seconds to compute pi with a precision of 10000 digits.
c# : 35 seconds to compute pi with a precision of 10000 digits.
c++ : 15 seconds to compute pi with a precision of 10000 digits.

As for propaganda, I see none - just the results of a benchmark.

0

Share this post


Link to post
Share on other sites
i don't want to comment on the benchmark a lot. quite frankly these kind of performance benchmarks don't mean so much, but yes they can be interesting in some cases. a pure number crunching benchmark should be winnable by a native language like c++, no surprise there. a realistic number cruncher would resort to simd instructions anyways which (for now) leaves all other alternatives far behind.. but that's only a matter of time (for me that is the main reason not to use managed languages for these kinds of applications. i need the simd capabilities the metal offers).
i like the fact, though, that people are starting to defend c# like most of us here do with c++ and the sc++l. that kind of advocacy for a certain language is a very good sign for the language (and - admit it! - its creators). i think that on its own is a good process.

cheers,
simon
0

Share this post


Link to post
Share on other sites
Quote:
Original post by barakus
Yeah, right. The fact that you only gave a decimal point only for the C# version (using a comma to represent it, making it look like a huge number) [...] implies that you hold a certain prejudice toward C#.
I think you're reading too much to that. In Europe (where he's from), believe it or not, people use comma to separate decimals (mind-boggling, I know!). And that only C# version had decimals is likely due to the fact that in C# he had to write the outputting code slightly differently. The C-version's timing and output is more of a direct port from the original D code (using the same printf statement).
Quote:
your dismissal of Promit's benchmark
I think this was fair. It's not like Promit's code is more idiomatic C#, it's just a tweak that happens to make .NET run faster. Similar tweaks could probably be found in C++ and D versions as well, and we could spend weeks optimizing each one. Why not actually compare versions that are near identical in code to save the trouble? It even looks to me like his code would make the algorithm wrong due to the fact that floating point math isn't as accurate, but I didn't check this..
0

Share this post


Link to post
Share on other sites
I've deleted the AP replies that seem intent on derailing this thread. So far the discussion is clean and interesting - whilst these benchmarks may not be the best way to compare languages they are a good platform for learning and discussing various characteristics.

The whole "," or "." thing is a bit pointless - why bother arguing over something so silly [smile]

Cheers,
Jack
0

Share this post


Link to post
Share on other sites
I'm not much of a .net freak but to me it seems like .net is going to be slower no matter what in this specific case. It's basically a series of tight loops with bounds checked array accesses which will generate at least an extra branch for each array access, probably with stack unwinding also.

I see nothing wrong with the test and it demonstrates what is a fairly well known weakness of .net, tight loops and a lot of array accesses.
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
It's not like Promit's code is more idiomatic C#, it's just a tweak that happens to make .NET run faster. Similar tweaks could probably be found in C++ and D versions as well, and we could spend weeks optimizing each one. Why not actually compare versions that are near identical in code to save the trouble? It even looks to me like his code would make the algorithm wrong due to the fact that floating point math isn't as accurate, but I didn't check this..


Different languages work in different ways. It is extremely biased to write a program in one language (C++) and expect a direct port to run equally fast on other languages (D or C#, in this case). Spending "weeks" (hopefully less) on optimizing each language according to their strengths would provide the best benchmarks. After all, I could care less how well C# or D implements C-specific algorithms. What I care about is the performance I'll achieve using those languages in the way they're designed.

- Mike
0

Share this post


Link to post
Share on other sites
Looks like DMD is missing some optimization oportunities in the div and mul functions. I tried using a similar modification to what Promit had posted:

import std.c.stdio;
import std.c.stdlib;
import std.c.time;

const int LONG_TIME=4000;

byte[] p;
byte[] t;
int q;

int main(char[][] args)
{
int startime, endtime;
int i;

if (args.length == 2) {
sscanf(&args[1][0],"%d",&q);
} else {
printf("Usage: pi [precision]\n");
exit(55);
}

if (q < 0)
{
printf("Precision was too low, running with precision of 0.\n");
q = 0;
}

if (q > LONG_TIME)
{
printf("Be prepared to wait a while...\n");
}

// Compute one more digit than we display to compensate for rounding
q++;

p.length = q + 1;
t.length = q + 1;

/* compute pi */

std.c.time.time(&startime);
arctan(2);
arctan(3);
mul4();
std.c.time.time(&endtime);

// Return to the number of digits we want to display
q--;

/* print pi */

printf("pi = %d.",cast(int)(p[0]));
for (i = 1; i <= q; i++)
printf("%d",cast(int)(p[i]));
printf("\n");
printf("%ld seconds to compute pi with a precision of %d digits.\n",endtime-startime,q);

return 0;
}

void arctan(int s)
{
int n;

t[0] = 1;
div(s); /* t[] = 1/s */
add();
n = 1;
do {
mul(n);
div(s * s);
div(n += 2);
if (((n-1) / 2) % 2 == 0)
add();
else
sub();
} while (!tiszero());
}

void add()
{
int j;

for (j = q; j >= 0; j--)
{
if (t[j] + p[j] > 9) {
p[j] += t[j] - 10;
p[j-1] += 1;
} else
p[j] += t[j];
}
}

void sub()
{
int j;

for (j = q; j >= 0; j--)
if (p[j] < t[j]) {
p[j] -= t[j] - 10;
p[j-1] -= 1;
} else
p[j] -= t[j];
}

void mul(int multiplier)
{
int b;
int i;
int carry = 0, digit = 0;

for (i = q; i >= 0; i--) {
b = (t[i] * multiplier + carry);
carry = b / 10;
digit = b - carry * 10;
t[i] = digit;
}
}

/* t[] /= l */

void div(int divisor)
{
int i, b;
int quotient, remainder = 0;

for (i = 0; i <= q; i++) {
b = (10 * remainder + t[i]);
quotient = b / divisor;
remainder = b - divisor * quotient;
t[i] = quotient;
}
}

void div4()
{
int i, c, d = 0;

for (i = 0; i <= q; i++) {
c = (10 * d + p[i]) / 4;
d = (10 * d + p[i]) % 4;
p[i] = c;
}
}

void mul4()
{
int i, c, d;

d = c = 0;

for (i = q; i >= 0; i--) {
d = (p[i] * 4 + c) % 10;
c = (p[i] * 4 + c) / 10;
p[i] = d;
}
}

int tiszero()
{
int k;

for (k = 0; k <= q; k++)
if (t[k] != 0)
return false;
return true;
}


Then I tested the original and modified program on two systems, on linux machine from my univ and my laptop running windows. Here are my results:

Linux 2.6.17-1.2142_FC4smp

Digital Mars D Compiler v0.168
dmd, no modifications: 50 seconds
dmd, modifications: 33 seconds
dmd flags: -release -inline -O

g++ (GCC) 4.0.2 20051125 (Red Hat 4.0.2-8)
g++, no modifications: 29 seconds
g++, modifications: 32 seconds
g++ flags: -O3



Windows XP SP2 Pro

Digital Mars D Compiler v0.168
dmd, no modifications: 20 seconds
dmd, modifications: 15 seconds
dmd flags: -release -inline -O

g++ (GCC) 3.4.2 (mingw-special)
g++, no modifications: 12 seconds
g++, modifications: 14 seconds
g++ flags: -O3

gdc (GCC) 3.4.2 (mingw-special) (gdc 0.19, using dmd 0.162)
gdc, no modifications: 12 seconds
gdc, modifications: 14 seconds
gdc flags: -frelease -finline -O3



I ran each test twice and took the better result. I used a windows build of the GCC D Compiler available from the following page: http://gdcwin.sourceforge.net/
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
I think this was fair. It's not like Promit's code is more idiomatic C#, it's just a tweak that happens to make .NET run faster. Similar tweaks could probably be found in C++ and D versions as well, and we could spend weeks optimizing each one. Why not actually compare versions that are near identical in code to save the trouble? It even looks to me like his code would make the algorithm wrong due to the fact that floating point math isn't as accurate, but I didn't check this..

Comparing identical versions is pointless unless you're comparing different compilers of the same langage. In other words, its only fair if you're comparing VC++ to g++. When comparing different languages, you have to make changes for each langage in order to keep the test fair...a tightly optimized program for language X may well run like shit in language Y. You should keep the algorithm the same, and that's it.

CM
0

Share this post


Link to post
Share on other sites
Quote:
Original post by doctorsixstring
Different languages work in different ways.
It's more of a comparison of compilers than languages here. As you can see, the codes are near identical. What we could conclude from Promit's tests is that the C++ compiler relieves the writer of the code from micro-optimizations more than the .NET compiler does. He did the same fix for C++ and the speed was unaffected. So when writing code casually for C++ without regard to micro-optimizations, you'd get 34/15 times faster code than with the .NET compiler. With .NET you'd have to spend extra effort doing the compiler's job to get down to 22/20. I think this already tells us something useful.
Quote:
What I care about is the performance I'll achieve using those languages in the way they're designed.
Can you claim with a straight face that Promit's opitimization is how you would've written the code in the first place with C#, and you would've used the modulo operator on C++ instead? It's not like C# was "designed" to perform worse with the integer modulo/div combo than C++. It's just a quirk.
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Conner McCloud
Comparing identical versions is pointless unless you're comparing different compilers of the same langage. In other words, its only fair if you're comparing VC++ to g++. When comparing different languages, you have to make changes for each langage in order to keep the test fair...a tightly optimized program for language X may well run like shit in language Y.
Even if we were comparing different compilers of the same language, one could use the same reasoning you do. One would want to optimize one's code for the particular compiler's quirks, no? And code tightly optimized for compiler X could run like shit when compiled with compiler Y.

For a code like this all three are essentially identical. None of them provide some super speed constructs that lack from the other languages that should be exploited for this code. Promit's changes still work for D and C++ directly. The original D code worked for C++ and C# (essentially) directly. I know there are tons of actual cases where language differences matter for speed, only this is not one of them. Promit's change was only to make the compiler happy, not to make the code "better C#".
0

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
It's more of a comparison of compilers than languages here.


Comparing a C# compiler to a C or C++ compiler to a D compiler is a bit like comparing apples to oranges, don't you think?

Quote:
Original post by Anonymous Poster
As you can see, the codes are near identical.


As I mentioned earlier, I think this is the wrong way to run a benchmark. What if the original example is written in a non-C language, and then ported to C in a way that is beneficial to the original language? Obviously that would be unfair.

Quote:
Original post by Anonymous Poster
What we could conclude from Promit's tests is that the C++ compiler relieves the writer of the code from micro-optimizations more than the .NET compiler does. He did the same fix for C++ and the speed was unaffected. So when writing code casually for C++ without regard to micro-optimizations, you'd get 34/15 times faster code than with the .NET compiler. With .NET you'd have to spend extra effort doing the compiler's job to get down to 22/20. I think this already tells us something useful.


I agree that C++ has the edge over C# in this case. It is interesting that C#'s modulus operator is so much slower that C++. What version of .NET was used for the tests? Has anyone compared it to non-MS implementations like Mono?

Quote:
Original post by Anonymous Poster
Can you claim with a straight face that Promit's opitimization is how you would've written the code in the first place with C#, and you would've used the modulo operator on C++ instead?


No, I honestly would never have thought to use Promit's code instead of the modulus.

Quote:
Original post by Anonymous Poster
It's not like C# was "designed" to perform worse with the integer modulo/div combo than C++. It's just a quirk.


I agree. It would be interesting to see if this quirk is common across all versions and implementations of .NET.

- Mike
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • By ilovegames
      The boy and the Golem were friends for a long time, and have traveled together.
      In search of the boy's father, they go to where he grew up, places now devastated by war.

      The player can at any time switch the characters and manage to solve a specific task.
      The boy is small and weak but is clever and an archer.
      Golem is big and strong, but slow, and prefers melees.

      The game includes the boom (required for archery) and apples (to restore his health). Golem Health is restored automatically. On the way our characters will encounter enemies and locked doors, and to open the door you must activate rock platforms nearby (decorated with blue stones that are activated by Golem from on the ground, and green that are activated from an arrow launched by the boy). Some doors will open only with simultaneous activation of a few (two or three) different types of stone platforms.

      Controls:

      Movement - W, A, S, D
      Switch character - Tab
      Direction of impact \ shot - mouse (cursor position)
      Attack:
      Golem - LMB
      Boy - When sandwiched RMB (Aim), press the LMB, for a shot
      Jump - Space



      TheBoyAndTheGolemSetup.exe
    • By KaiZerg
      Dear Colleagues,
      Can you please advise on the issue below:
      I have got several Folders(paper), to keep cheat sheets, practical guides, Lists of sequences ect.
      What is the best way to separate Folders?
      1)
      -Unity all
      -Unreal Engine all
      2)
      -Game art
      -Game Design
      -Game Engine
      -Game Programming
       
    • By suerozcelik
      Hi everyone, My name is Arda Süer, i’m a game developer and producer of Pocket Universe : Create Your Community. Pocket Universe is on pre-order and soon the early access to Steam. I need your help and I want to introduce you to my game. Please read the game story and website content etc. If you have any suggestions tell us and we can develop. Example:"Story is good but you need more faction","You need add bla,bla to the game". Anyway I will put links here to help you to understand Pocket Universe. Story : http://www.pocketuniversegame.com/?page_id=7 Features : http://www.pocketuniversegame.com/?page_id=180 Factions : http://www.pocketuniversegame.com/?page_id=54 Press Release : http://www.pocketuniversegame.com/?page_id=237 If you want to help more, please buy my game Kinguin : https://www.kinguin.net/category/38680/pocket-universe-create-your-community-pre-order-steam-cd-key/ G2A : https://www.g2a.com/pocket-universe-create-your-community-steam-key-global-i10000070716001 Press Relase.rtf
    • By NA-45
      I'm currently working on Metroidvania style game that I was inspired to start by Hollow Knight and Beksiński's art.

       It's built in Unity using C# and has quite a bit done already.  I'm handling the programming myself and have a working model (besides combat which is a WIP) that can be expanded greatly depending on where we decide to take the project.  You can see the current test area here: https://streamable.com/mp5o8  Since I'm not artistically gifted, its all rectangles but can easily be skinned once we've desired on designs.
      I have professional experience using Unity and C# working on both a released game and a prototype as well as having extensive Java knowledge.  I also dabble in Python with a little bit of C++.
      I have worked on and completed many projects before, the most recent being a 2D stick fighting game written ground up in Java Swing (don't ask why): https://www.youtube.com/watch?v=V4Bkoyp_f0o
      I'm looking for a 2D artist (potentially more than one) to create concept and game art and a designer/writer who can help flesh out the story as well as map out and create challenging and eye catching areas.  I can handle most if not all of the programming side of things though if there is anyone who is extremely passionate about this sort of thing, I'd consider splitting the load.
      The end goal is a completed game that can be sold however profit isn't really a concern to me as it's mostly a labor of love from my part.  Any profits would be split between team members however that's pretty far off so don't make that a reason to join.
      ______________________________
      The story I have in mind is something like this:
      A man wakes up in a chasm that stretches seemingly endlessly in both directions lined with enormous statues.

       He discovers a temple with text above a closed gate that tells of the failed kingdom that lies below.  After finding a way around this, he drops down into the subterranean kingdom.  Adventuring through the labrynth below, he comes across different cities in which the residents succumbed to different sins such as Greed, Wrath, etc.  Each city tells a story of how its fixation on something lead to their demise leading up to a fight with the personification of their mistake.
      ______________________________
      An very rough idea for Waterways, a potential area:
       - To enter you must be wearing a pair of glasses that you find somewhere earlier in the ruins.  There are similar glasses found in every home.  Everything appears incredibly beautiful however something seems wrong.  After triggering some event, the glasses break and it's revealed that the glasses are made of some sort of stone that makes everything appear differently.  The city is in ruins and absolutely disgusting as everything was neglected.  
       - The only thing that remains intact is in the center of the city, an incredible statue of a goddess holding up a large sphere of the same material that was used for the glass.  You slowly learn the story behind the statue: the goddess came from the sea that the city lies on and brought prosperity to them.  
       - After opening up the the temple of the goddess that lies right on the edge of the waters, a giant sheet of the glass covers an opening in the back of the temple that reveals the goddess behind it.  You shatter the glass and it becomes apparent that the goddess is actually a disgusting creature half beached and mostly immobile that appears to secrete the material that makes up the glass. Fight ensues.
      ______________________________
      The combat is pretty up in the air and part of the reason I need a designer to bounce ideas off of but I think it will be something like this:
       - 4 orbs equipped at a time
       - 2 orbs selected at a time
       - Pressing the cast button will cast a spell determined by the 2 orbs that are selected
       - Spells cost mana however you can use spells with 0 mana and it will cost health instead
       - These spells in addition to being useful for combat, are the Metroidvania "gating" metchanic.  For instance, one of the conceptualized spells is a water orb + water orb to create a ice pillar that can be either used to block projectiles/enemy paths or to jump on to reach high areas
      ______________________________
      If you're interested or have any questions, contact me through discord.  My id is NA-45#3692. 
       
    • By Qwertyman
      My game (built on the Unity platform, making use of EZ GUI) requires players to log into Facebook. However, when the player is using a language other than English on Facebook, I'm unable to display the localized Username (which is the same as the FB Username) within the game. Any ideas on how this can be tackled?
      PS: Detecting the OS language doesn't really help here because, for all you know, the player might be using one language for the OS, but another one for their Facebook, so.... 
      TIA!
  • Popular Now