C# AI Interface, and Proof of concept C# AI - Page 4

C# AI Interface, and Proof of concept C# AI

Here is where ideas can be collected for the skirmish AI in development

Moderators: hoijui, Moderators

User avatar
jcnossen
Former Engine Dev
Posts: 2440
Joined: 05 Jun 2005, 19:13

Post by jcnossen »

You're free to make your own stuff in python, different people like different languages...
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Post by imbaczek »

I know, and I don't dispute that. I'm refuting some claims which are in contradiction with reality. I like Python and am developing in it basically daily, but can't and won't force anybody to use it; what I can do is explain some people that they, let's say, misunderstand it :wink:
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

There are times when even the C++ code runs slow in AI, and I'm sure such a setup with a C module in a python app would introduce overhead.

But just for you I'll take a look at IronPython when OSRTS rolls around.
User avatar
hughperkins
AI Developer
Posts: 836
Joined: 17 Oct 2006, 04:14

Post by hughperkins »

Language wars....

Python is an excellent scripting language, it's fast, nice syntax, well established community.

C++ is an excellent compiled language. There are lots of libraries, it runs very quickly, huge community.

C# is as fast as C++ and as easy as Python, and has a huge community.

Wrt ease of use, a big difference between C++ and C# is garbage collection and bounds checking. No more memory leaks, or stack/ heap corruption issues.

Wrt speed, here's a prime number generator in both C++ and C#. Try compiling and running them, and compare the results.

C++ version

Code: Select all

#include <iostream>
#include <ctime>
using namespace std;

int CalculateNumberOfPrimes( int maxprime )
{
    bool *IsPrime = new bool[ maxprime ];
    
    for( int i = 0; i < maxprime; i++ )
    {
        IsPrime[i] = true;
    }
    
    int NumberOfPrimes = 0;
    
    for( int i = 2; i < maxprime; i++ )
    {
        if( IsPrime[i] )
        {
            NumberOfPrimes++;
            for( int j = ( i << 1 ); j < maxprime; j+= i )
            {
                IsPrime[ j] = false;
            }
        }
    }
    
    return NumberOfPrimes;
}

int main( int argc, char *argv[] )
{
    clock_t start = clock();
    
    int NumberOfPrimes = CalculateNumberOfPrimes( 10000000 );
    cout << "number of primes: " << NumberOfPrimes << endl;
    
    clock_t finish = clock();
    double time = (double(finish)-double(start))/CLOCKS_PER_SEC;
    cout << "elapsed time: " << time << endl;
    
    return 0;
}
C# version

Code: Select all

using System;

class Primes
{
    public int CalculateNumberOfPrimes( int maxprime )
    {
        bool[] IsPrime = new bool[ maxprime ];
    
        for( int i = 0; i < maxprime; i++ )
        {
            IsPrime[i] = true;
        }
        
        int NumberOfPrimes = 0;
        
        for( int i = 2; i < maxprime; i++ )
        {
            if( IsPrime[i] )
            {
                NumberOfPrimes++;
                for( int j = ( i << 1 ); j < maxprime; j+= i )
                {
                    IsPrime[ j] = false;
                }
            }
        }
        
        return NumberOfPrimes;
    }
}

class EntryPoint
{
    public static void Main()
    {
        System.DateTime start = System.DateTime.Now;
    
       int NumberOfPrimes = new Primes().CalculateNumberOfPrimes( 10000000 );
    
       System.DateTime finish = System.DateTime.Now;
       double time = finish.Subtract( start ).TotalMilliseconds;;
        
       Console.WriteLine( "number of primes: " + NumberOfPrimes );
       Console.WriteLine( "elapsed time: " + ( time / 1000 ) );
    }
}
On my machine I get:

C++ version:
H:\dev\test\CSharpAI>H:\dev\test\testperf\prime.exe
number of primes: 664579
elapsed time: 2.265

C# version:
H:\dev\test\CSharpAI>H:\dev\test\testperf\primecs.exe
number of primes: 664579
elapsed time: 2.03125

Admittedly, optimizations are turned off by default in cl, and turned on by default in C#. Nevertheless, these execution times are clearly not miles apart.
User avatar
hughperkins
AI Developer
Posts: 836
Joined: 17 Oct 2006, 04:14

Post by hughperkins »

Addendum, as AF is close to pointing out, you can use the C++/C# interface to run Python, using Boo or IronPython.

Whilst the C++/C# interface is called C++/C# interface, actually it should really be called the C++/.Net interface. It will in theory work with any .Net language out there, and IronPython and Boo are both .Net languages.

For info, here's a full list of languages that will probably work in Spring Global AIs, via the C++/.Net interface:

http://www.dotnetpowered.com/languages.aspx
User avatar
hughperkins
AI Developer
Posts: 836
Joined: 17 Oct 2006, 04:14

Post by hughperkins »

Created a boo GlobalAI HelloWorld, see http://taspring.clan-sy.com/phpbb/viewtopic.php?t=7865
User avatar
hughperkins
AI Developer
Posts: 836
Joined: 17 Oct 2006, 04:14

Post by hughperkins »

User avatar
Licho
Zero-K Developer
Posts: 3803
Joined: 19 May 2006, 19:13

Post by Licho »

Primes speed C# vs. C++ .. wow!

What the hell is this?? How can it be faster.. Have you looked at generated assembly code? C++ code "looks" much faster, it's 2-3x shorter .. whole core of procedure has just 16 instructions .. and c# has 2-3x more and looks messy (calls instead of jmps and such) and still it runs faster... How is that possible?
JIT compiler must be much more better than C++ compiler .. weird..
danzel
Posts: 56
Joined: 30 Sep 2005, 01:49

Post by danzel »

I think you'd find it'd be in C++'s favour if optimizations were on for cl (c++ compiler)

It would still be close however, JIT compilers are pretty damn smart these days. I remember reading a paper on making unoptimized compiled code run faster by running the compiled binary on a JIT compiler. :D
User avatar
Licho
Zero-K Developer
Posts: 3803
Joined: 19 May 2006, 19:13

Post by Licho »

Well I compiled it with full optimalizations for c++
pheldens
Posts: 191
Joined: 12 Aug 2006, 21:35

Post by pheldens »

I think C# / mono is a stupid choice, it's future is uncertain for FLOSS projects.

maybe Lua, ruby, or python are suited.
User avatar
jcnossen
Former Engine Dev
Posts: 2440
Joined: 05 Jun 2005, 19:13

Post by jcnossen »

You need speed as well as flexibility...
why do these language discussions keep coming up :S
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

lua lacks the necessary language features todo what people have tried todo in C++ for spring AI's and Python is just too slow and a strongly typed language such as java C# or C++ is actually helpful imo.

And incase you havent been reading the news, microsoft and novel have some new partnership that protects SuSE and all novells linux assets from microsoft patent lawsuits and other legal issues that could arise from the two companies, which means mono (as a novell project) is safe from the wrath of microsoft. And C# usage is on the rise, and microsoft has a future for it. Did you know that in XP the core is win32 API with .Net running ontop, yet on Vista its the other way round? With .Net 3.0 due to arrive and a hefty budget for research and development, I'd say C# is as good as any a language if not better.

So why is everyone insisting we should throw away our .Net bindings and go for the inferior lua or the slower python and start from scratch when all we have todo is use an IronPython or a Lua .Net assembler instead of a C# compiler?

And you do realize that using .Net takes away something like 90% of the work needed to port from spring to OSRTS?
bamb
Posts: 350
Joined: 04 Apr 2006, 14:20

Post by bamb »

Hey, I don't know C# but sounds good...

For example, Java has a lot of good things over C and C++. Namely modernity, good documentation and consistent libraries and in general making a lot of sense. I personally know C much better and it's faster and all, but there are things in Java that are much more comfortable than in current C or C++.

But maybe C# will be the thing in the future, everything doesn't need to be done on so damn low level and error-pronely like in C++. String handling for example is pure hell in C++ from what I remember doing it way back... With high-level script languages like Matlab with consistently designed helper functions it's so damn easy and you make a thousand mistakes less, your code is short and easy to read and is portable too. (The loops are slow :( But often you can avoid them! 8) )
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

C style const char* strings arent very nice to work with, though I like the std::string type that comes with C++, the java version has more options though and is generally prettier and doesnt require as many extra functions be written such as tolowercase() or equalsignorecase(), which is why I like C# and Java/J#, I just wish Swing was better and JNI wasnt so awkward.
pheldens
Posts: 191
Joined: 12 Aug 2006, 21:35

Post by pheldens »

AF wrote: And incase you havent been reading the news, microsoft and novel have some new partnership that protects SuSE and all novells linux assets from microsoft patent lawsuits and other legal issues that could arise from the two companies,
Thats exactly what I am referring to, but in reverse :)
But I see, regardless of one's opinion on that case, there is atleast awareness about it, Just thought to mention it.
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

It seems redhat not C# is the looser in this case
Tobi
Spring Developer
Posts: 4598
Joined: 01 Jun 2005, 11:36

Post by Tobi »

std::string sucks too IMHO. They forgot to add all functions you actually need for strings, and just added all kinds of functions you usually don't need.

The only good string class I know is QString ;-)
User avatar
Licho
Zero-K Developer
Posts: 3803
Joined: 19 May 2006, 19:13

Post by Licho »

Well I'm seasoned C++ developer, I'm still relatively new to C# and all I can say is that C# increased my productivity many times.. It's just so much easier to debug and code with framework libraries behind your back.
Most of the annoying bugs don't appear or are detected instantly and correctly. I remember weeks spent on some stupid memory overwrite bugs in C++ (with huge projects like a game). In c# nearly all bugs can be fixed instant they appear amd unless you do very stupid things, memory leaks are not a problem anymore.
I also like events and delegates and properties and interfaces and other language goodies (lock(this), using (new Resource), foreach, attributes) it's simply joy to code. C++ is often hell :)
Only thing I seriously miss is const (const methods and method parameters and such).
I also used to miss precompiler and templates but now I think it's better the way it is with generics and stuff... code is less messy than C++ can become.

All the time I had spent coding slowly in C++ and hand optimizing some ASM for various CPUs seems like stone age now..
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Post by imbaczek »

It's not hard at all for a language to be easier to program in than C++. C++ is the most complicated language I've seen, or even heard of. Even Lisp is easier to learn.

C# is really Delphi brought up to date and with C++-ish/Java-ish syntax :P Most of it's power comes from the CLR and the huge .NET standard library - and don't get me wrong, that's a good thing :)
Post Reply

Return to “AI”