Freebasic v's C# code execution speed ?

General discussion for topics related to the FreeBASIC project or its community.
TESLACOIL
Posts: 1769
Joined: Jun 20, 2010 16:04
Location: UK
Contact:

Re: Freebasic v's C# code execution speed ?

Post by TESLACOIL »

My OP reads

Freebasic v's C# code execution speed ? ....does anyone here have C# installed ?

Id like to know how fast the for next loops are and other simple commands like Print "hello" or line (x,y)-(x,y) are by comparison



Can i assume from your replies that none of you have C# installed ?

if so fine, ill find someone who does have C# installed...or just reinstall it myself




this entire post ,could all have been over within 5 minutes
Last edited by TESLACOIL on Jun 05, 2012 17:54, edited 1 time in total.
mambazo
Posts: 652
Joined: Jul 17, 2005 13:02
Location: Ireland
Contact:

Re: Freebasic v's C# code execution speed ?

Post by mambazo »

I have it installed, but just like you, I can't "be arsed" ;)

You could have installed it by now, by the way.
TESLACOIL
Posts: 1769
Joined: Jun 20, 2010 16:04
Location: UK
Contact:

Re: Freebasic v's C# code execution speed ?

Post by TESLACOIL »

tell me about it ,lol


Ive got a bud who uses C# but i wont see him for a few weeks, we will write some speed tests then






wish i never asked (slaps forehead )

carry on all, as you where
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Re: Freebasic v's C# code execution speed ?

Post by 1000101 »

In case you really do want to do benchmarks, it is important to note that benchmarks are only applicable to algorithms, not compilers. You create multiple algorithms to complete the same task and benchmark those. Benchmarking how many times the CPU can increment a memory location is not a benchmark and can be calculated from the hardware documents anyway.

You really don't know what a benchmark is or what it is used for. Stress testing your ALU is not a benchmark.
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Re: Freebasic v's C# code execution speed ?

Post by Gonzo »

honestly, in robotics and game-making the bottlenecks eventually should be multi-threading time-spent-waiting, or lack of multithreading :P
it probably applies off the board, but as im making a game engine right now, i can tell that my mutex-lock stuff probably isnt optimal
i need to find a lock-free solution, or make some kind of simple queueing
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: Freebasic v's C# code execution speed ?

Post by MichaelW »

TESLACOIL wrote:each command or code block will have a typical execution speed

if you want to test real world speed of a computer program written in a specific language this is one way to do it

each language will be more efficient in some respects than others, that's a given, even a simple for next loop can be very revealing as to the capabilities of the computer and the language that the for next loop is written in

dont underestimate the power of simple tests


more complex tests say on sorting, data , graphics manipulation will reveal wider trends, the simpler tests will pull up specific deficiencies
I think that in recent times well-developed mainstream high-level languages/compilers generally do not have significant performance deficiencies. Your time would be much better spent benchmarking algorithms.

And if you do bother to compare loop times between FreeBASIC and C#, and it looks like the C# code is much faster, you need to look closer. To an optimizing compiler empty loops and other code where the results are not used are effectively “dead” code, so an obvious optimization is to remove them.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Re: Freebasic v's C# code execution speed ?

Post by anonymous1337 »

TESLACOIL:

Sounds like you want people to do work for you, then you get confused when they don't like how that feels. We're not a consultation forum. If you want a business/technology consultant, I cost about $xx an hour. If that's a problem for you, DIY.

"Simple speed test" - Since when were accurate benchmarks ever that simple?

You also talk like a manager/business guy, and I'm aware that you have expertise in certain areas. I'm also aware that a lot of that information is outdated or has been superceded by modern practices.

It'd just be nice if you were willing to listen to our feedback, rather than acting like we're being lazy, unmotivated, or worse, don't know what we're talking about. Appreciating us for our skill, you come here and ask questions. Full of hubris, you ignore what we say.
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Re: Freebasic v's C# code execution speed ?

Post by 1000101 »

Gonzo wrote:...i can tell that my mutex-lock stuff probably isnt optimal
i need to find a lock-free solution, or make some kind of simple queueing
I have a freebasic implementation of reader-writer locks. They are very fast non-blocking read locks and a invoke a mutual exclusion on write locks (many reader, one writer). I have also been working on a lockless queue for data synchronization, however it requires the CPU to support the cmpxchg8b instruction and is x86 only, but who isn't running at lease a P5 + extension or a P6 or higher? Since it relies on an instruction well established (almost 20 years now) that works atomically, (thus the worst-case is a retry on the update) it is much faster then any locking method (which all require high-level support (eg, mutexes)) and thus your miss on the update is measured in cycles (the amount of retries to be successful) and not in human measurable time slices.

The rwlock code was ported from C, you can find it here. Although it is win32 only linux already natively supports many different concurrency controls including rwlocks.
ciplogic
Posts: 19
Joined: Jun 05, 2005 9:26
Contact:

Re: Freebasic v's C# code execution speed ?

Post by ciplogic »

There are many way to evaluate performance and in many times is irrelevant, but if you want real numbers is simple: find the problem you want to solve and see if your language of choice (I think FB) solves your problem in timely manner.
C# it should generate slower code overall but in a small margin, when compared against GCC backend in computational intensive codes. There is literature to compare C# with C/C++ and you will find like following numbers:
- in integer based codes C# is in the range of C/C++ (around 10-30% slower, sometimes is faster)
- in floating point matrix multiplication, C++ is some times faster if it match the autovectorizer optimization of the compiler
- in floating point when the compilers do not match any optimization they would work in the same range as integer based codes
- a lot of classes that are allocated on heap would run faster than C/C++. If you're using smart-pointers (reference counting) for C++ side) it would work even slower by many times than C#
- a lot of algorithms in the .Net framework library are well implemented and fairly fast(for reflection, xml processing, and so on) and if you are not looking to a good implementation on C++ side, or you make your own, you may find that your implementation may work slower than the C# side.
At the end, C# side is notoriously slow on startup as it has to decode the assemblies, to apply some optimization passes because of JIT step. This performance hit can be circumvented by using the right tool (NGen), but for a developer who is unaware, and not willing to optimize the startup time for a complex application, it may not be desirable to use C#. This hit of performance and the JIT time unpredictability can mean that some high complexity games may not be well intended for C# (at least if you don't have ways to pre-JIT your code), and can be a unsatisfactory experience.
At the end, if you are a beginner, I do recommend to you FreeBasic over C# not because of performance of either of them, but because you will have fewer things to know. C# is somewhat easy to start but hard to master, because of many technologies you should know to make an application working. Also C# used in a context like small games, would have a framework overhead which if you are not knowledgeable about PInvoke and other technology limitations, could mean that you will not be able to have your desired work.
A last small note: I'm a professional ex-C++ developer for 5 years, and another 3 in C# world. C# experience is gratifying, but my response is targeted for a beginner. I'm working with Ruby too, and even its performance is order of magnitude slower than both C# and C++, is good enough for a lot of usages. At the end I'm thinking that benchmarking is misleading in most of the ways, but people went well beyond it.
marcov
Posts: 3503
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Freebasic v's C# code execution speed ?

Post by marcov »

TESLACOIL wrote:My OP reads
does anyone here have C# installed ?
Which one on which architecture (VMs react differently to x86->x86_64 changes than statically compiled languages) ?

Other border conditions?
Id like to know how fast the for next loops are and other simple commands like Print "hello"
What does that have to do with language? That just jumps directly to the system console output routine?
or line (x,y)-(x,y) are by comparison
Very dependent on canvas type.
codeFoil
Posts: 256
Joined: Dec 22, 2011 4:45
Location: United States
Contact:

Re: Freebasic v's C# code execution speed ?

Post by codeFoil »

marcov wrote:
TESLACOIL wrote: Id like to know how fast the for next loops are and other simple commands like Print "hello"
What does that have to do with language? That just jumps directly to the system console output routine?
Which in 9 out of 10 languages/platforms will lead to you to the same C run time library.
marcov
Posts: 3503
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Freebasic v's C# code execution speed ?

Post by marcov »

codeFoil wrote:
marcov wrote: Which in 9 out of 10 languages/platforms will lead to you to the same C run time library.
I think that is a Unix centric generalization that is not so ubiquitous as many people think.

This is easily demonstrated by the fact that the global majority platform (windows) has not one but multiple C run time libraries, and usage of it is not part of the documented OS API, but part of the C/C++ development system (most 3rd party development systems that are not unix ports directly work with kernel32/user32, not with MSVCRT, if only because the msvcrt version of your choosing might not be installed in a few years. msvcrt not being a part of the portable (from win95 to windows 8) win32 api).

And even on Unix, it depends on what code you generate. If you generate a printf statement to write a simple string, you jump into a routine, but if you optimize it to emit a write() it might directly do the kernel call, but that skips buffering.
Last edited by marcov on Jun 09, 2012 19:45, edited 2 times in total.
TESLACOIL
Posts: 1769
Joined: Jun 20, 2010 16:04
Location: UK
Contact:

Re: Freebasic v's C# code execution speed ?

Post by TESLACOIL »

lol , not being lazy....i just have a thang about running the numbers...though not everyone will be as geeky about real world benchmarks as i am

when i hook up with my fellow geek bencher in RL ill get some side by side FB vs C# code written

we do lots of ' how fast is it really ? tests ' all the time.....and yes wiggling yer mouse like crazy can chew up to 5% of cpu cycles



its a drag racing gene , car boats, motorbikes, computers.....if its got a speed dial im watching it lol




Its also partly related to my AI project and the choice of languages available to recode certain modules. Some parts of the network have a lot more CPU headroom than others and it would be nice to farm off some of the smaller modules that need recoding. I have RL links with some C & C# coders so i may bung them a crate of beer or three. Not so easy to communicate over the net or send payments. Exchanging folding stuff over a cool beer works well for me....partially explains my keen interest in the minutia of all things FreeBASIC / performance wise

*the complexity of the AI system is in the HW & SW architecture....so using FreeBASIC to develop the software eases brain strain for me. Yes ideally i need uber code speed but high readability has to take precedence....but its good to know the lines running through the ball park ref unoptimized but readable code vs optimized code and the ballpark losses or gain with a given language. * some of the modules are specific keyword heavy, so comparative performance is nice to know from my perspective. I can then configure the auto-coders to output in a specific language if there are significant speed or readability gains to be had.

as AMD has dropped out of the speed race its Intel's x86 et al architecture that will do the heavy lifting on the HW side , one less issue i have to worry about, though the next few installment of M$ windblows prolly wont see Santa's sack filled with thank you messages from me, lol
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: Freebasic v's C# code execution speed ?

Post by MichaelW »

I have zero interest in C#, so I’m not willing to download and install it and then figure out how to do a meaningful comparison to FreeBASIC, assuming that it’s even reasonably possible. But I can easily do a comparison between code optimized (with /O2 /G6) by the Microsoft C compiler and code compiled with FBC, in this case between almost-empty for loops. The loops are almost-empty to avoid having the C compiler “optimize” the loop by removing it.

The C macros are available here:

http://masm32.com/board/index.php?topic=261.0

And the FreeBASIC macros (COUNTER.BAS) are here:

http://www.freebasic.net/forum/viewtopi ... =7&t=20003

The C code:

Code: Select all

//=======================================================================================
#include <windows.h>
#include <conio.h>
#include <stdio.h>
#include "counter_c.c"
//=======================================================================================
void main( void )
{
    int i,j,x=0;
    SetProcessAffinityMask( GetCurrentProcess(), 1);

    Sleep(5000);

    for( i=0; i < 4 ; i++)
    {
        counter_begin( 1, 10000000, REALTIME_PRIORITY_CLASS, THREAD_PRIORITY_TIME_CRITICAL );
        counter_end( 1 )
        printf("%d cycles\n", counter_cycles );

        counter_begin( 2, 10000000, REALTIME_PRIORITY_CLASS, THREAD_PRIORITY_TIME_CRITICAL );
            for( j=0; j < 10; j++)
                x += 1;
        counter_end( 2 )
        printf("%d cycles\n\n", counter_cycles );
        Sleep(1000);
     }
     printf("%d\n", x );
    getch();
}
And the FreeBASIC code:

Code: Select all

''=============================================================================
#include "counter.bas"
''=============================================================================

dim as integer r, x

sleep 5000

SetProcessAffinityMask( GetCurrentProcess(), 1)

for i as integer = 1 to 4
    counter_begin( 10000000, REALTIME_PRIORITY_CLASS, THREAD_PRIORITY_TIME_CRITICAL )
    counter_end()
    print counter_cycles;" cycles"
    counter_begin( 10000000, REALTIME_PRIORITY_CLASS, THREAD_PRIORITY_TIME_CRITICAL )
        for i as integer = 0 to 9
            x += 1
        next
    counter_end()
    print counter_cycles;" cycles"
    print
    sleep 1000
next
print x
sleep
Typical results for the FreeBASIC code compiled with 0.23.0 with -arch set to the default 486, 686, or pentium3, running on a P3:

Code: Select all

 0 cycles
 55 cycles

 0 cycles
 55 cycles

 0 cycles
 55 cycles

 0 cycles
 56 cycles

 400000000
Typical results for the FreeBASIC code compiled with 0.23.0 with -arch set to the default 486, or pentium4, running on a P4 (Northwood):

Code: Select all

 0 cycles
 67 cycles

 0 cycles
 67 cycles

 0 cycles
 68 cycles

 0 cycles
 69 cycles

 400000000
Typical results for the C code running on a P3:

Code: Select all

0 cycles
76 cycles

0 cycles
76 cycles

0 cycles
76 cycles

0 cycles
76 cycles

400000000
And typical results for the C code running on a P4 (Northwood):

Code: Select all

0 cycles
37 cycles

1 cycles
40 cycles

0 cycles
43 cycles

1 cycles
38 cycles

400000000
I think it would be more meaningful to time other code, for example the popcount code that I used to test the new cycle count macros.
Last edited by MichaelW on Jun 10, 2012 9:26, edited 1 time in total.
TESLACOIL
Posts: 1769
Joined: Jun 20, 2010 16:04
Location: UK
Contact:

Re: Freebasic v's C# code execution speed ?

Post by TESLACOIL »

could you post up the code for counter.bas

i have a large variety of machines i can run your code on , from Atom to i5 2500k

Benchmarks & utilities
http://asimov1.wikispaces.com/Download+Benchmarks

Fritzbench will give you a feel for the general speed of a particular computer. Speedtest7.exe yields a similar results spread to Fritz when Fritz is set to one core




you might find the CPUID code here useful
http://en.wikipedia.org/wiki/CPUID
Post Reply