C# AI Interface, and Proof of concept C# AI
Moderators: hoijui, Moderators
- hughperkins
- AI Developer
- Posts: 836
- Joined: 17 Oct 2006, 04:14
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
C# version
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.
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;
}
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 ) );
}
}
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.
- hughperkins
- AI Developer
- Posts: 836
- Joined: 17 Oct 2006, 04:14
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
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
- hughperkins
- AI Developer
- Posts: 836
- Joined: 17 Oct 2006, 04:14
Created a boo GlobalAI HelloWorld, see http://taspring.clan-sy.com/phpbb/viewtopic.php?t=7865
- hughperkins
- AI Developer
- Posts: 836
- Joined: 17 Oct 2006, 04:14
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..
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..
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?
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?
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!
)
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


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.
Thats exactly what I am referring to, but in reverse :)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,
But I see, regardless of one's opinion on that case, there is atleast awareness about it, Just thought to mention it.
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..
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..
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
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 :)
C# is really Delphi brought up to date and with C++-ish/Java-ish syntax
