SFML community forums

General => General discussions => Topic started by: DarkRoku12 on May 26, 2015, 07:24:44 pm

Title: Incredible performace of Lua (luajit) over C++
Post by: DarkRoku12 on May 26, 2015, 07:24:44 pm
I'm making a lua-sfml engine and i decide to test the performace making the same code on pure C++.

All compiler optimizations are on on both projects ( -O3 -fexpensive-optimizations ). (using codeblocks)

Not frame limit in both projects.

Lua-jit gets version  11 fps stable ( with  1000 crates on display , physics engine active ( there are 3 physics object active in the scene, some events dispatcher actives )

In C++ pure version got 8 fps stable ( with ONLY 1000 crates on the display..no physics engine active..no Lua states actives )

I use 2 counters for the test.. a custom counter on the game..and FRAPS.. both counter say the same fps rate. ( 11 for  lua / 8 for  c++ )

C++ code for the test only this no more:
#include <iostream>

using namespace std ;
#include <SFML/Graphics.hpp>
using namespace sf ;

int main()
{
    // create the window
    sf::RenderWindow window(sf::VideoMode(600, 600), "My window") ;
    int fps_rate = 0 ;
    Clock *fps = new Clock ;
    sf::Texture t ;
    // load the texture
    t.loadFromFile( "crate.png" ) ;
    t.setSmooth( true ) ;

    Sprite *obj = new Sprite[ 1000 ] ;
    for( int s = 0 ; s < 1000 ; ++s )
           obj[s].setTexture( t ) ;

    // run the program as long as the window is open
    while (window.isOpen())
    {
       
       // fps counter
        ++fps_rate ;
        if ( fps->getElapsedTime().asSeconds() >= 1 )
        {   fps->restart() ;
            cout << fps_rate << endl ;
            fps_rate = 0 ;
        }

        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();
        }

        // clear the window with black color
        window.clear(sf::Color::Black);

        // draw everything here...

        for( int i = 0 ; i < 1000 ; ++i )
        {
            window.draw( obj[ i ] ) ;
        }

        // end the current frame
        window.display();
    }

    return 0;
}
 


Screenshots:

lua version

(http://www.subirimagenes.com/imagedata.php?url=http://s2.subirimagenes.com/imagen/9376732luaversion.png)

The small crate and the long crates are physics objects. (the small crates is falling)
The bigs crates ( there are one thousand , you cant see it because are one behind other )
The console show the fps..and the yellow numbers are shown by FRAPS.

The same in the C++ version :

(http://www.subirimagenes.com/imagedata.php?url=http://s2.subirimagenes.com/imagen/9376734c.png)

As you can see.. no physics..no lua States..

Please..if you can optimizate more this c++ code please post it for test it.

I would like yours reviews. :)

PD: link for the full images:

c++ : http://www.subirimagenes.com/imagedata.php?url=http://s2.subirimagenes.com/imagen/9376734c.png

lua : http://www.subirimagenes.com/imagedata.php?url=http://s2.subirimagenes.com/imagen/9376732luaversion.png

Lua game engine topic: http://en.sfml-dev.org/forums/index.php?topic=18074.0
Title: Re: Incredible performace of Lua (luajit) over C++
Post by: Jabberwocky on May 26, 2015, 07:30:48 pm
Weird.
Are you running the C++ version from VisualStudio (or CodeBlocks it looks like)?  Even in release, that can be significantly slower.  If so, try running by double-clicking the executable instead.
Title: Re: Incredible performace of Lua (luajit) over C++
Post by: DarkRoku12 on May 26, 2015, 07:34:00 pm
Weird.
Are you running the C++ version from VisualStudio (or CodeBlocks it looks like)?  Even in release, that can be significantly slower.  If so, try running by double-clicking the executable instead.

Still the same for both.
Title: Re: Incredible performace of Lua (luajit) over C++
Post by: Nexus on May 26, 2015, 07:34:43 pm
Quote
Incredible performace of Lua (luajit) over C++
Whenever I read such a sensational headline, I'm almost sure the measurement is distorted. There are JIT compilers that can outperform C++ in certain situations, but people who do so come with more analytic ways of presenting their results ;)

Indeed, there are several issues with your C++ code, making the measurement meaningless:
And what's the point of
Clock *fps = new Clock;
Sprite *obj = new Sprite[ 1000 ] ;
when you can just use
Clock fps;
std::vector<Sprite> obj(1000);
without the need for manual memory management? Use RAII (http://www.bromeon.ch/articles/raii.html) where possible.
Title: Re: Incredible performace of Lua (luajit) over C++
Post by: DarkRoku12 on May 26, 2015, 07:40:27 pm
There are several issues with your C++ code, making the measurement meaningless:
  • You should measure frame time, not FPS.
  • Having a std::cout in the measurement slows down everything, might as well be the bottleneck.
  • If you're really going for CPU performance and not just the GPU communication overhead, use fewer draw calls (sf::VertexArray instead of sf::Sprite).
  • Use iterators or range-based for instead of index access. Make also sure that any debug checks in the STL are disabled.
And what's the point of
Clock *fps = new Clock;
Sprite *obj = new Sprite[ 1000 ] ;
when you can just use
Clock fps;
std::vector<Sprite> obj(1000);
without the need for manual memory management? Use RAII (http://www.bromeon.ch/articles/raii.html) where possible.



I replace all the code with the one that you give me.
Even i take off the fps counter and "cout" ..still the same..sometime ups to 10 fps (FRAPS) but then slow to 8 again.

i use memory management because i like C style.
Title: Re: Incredible performace of Lua (luajit) over C++
Post by: Nexus on May 26, 2015, 07:47:41 pm
You're the one wanting to prove something, not me ;)
I think it should not be difficult to apply my suggestions. And you're still measuring FPS, not frame time.

Where is the Lua code? How do we know it's equivalent?
A meaningful measurement would also include a minimum and average frame time aggregated over a longer time, not just random FPS sampled once a second.

Btw, I've also edited my post. Please avoid full quotes, it makes the thread hard to read.


i use memory management because i like C style.
If you have some time, you could read the RAII article I linked to, there are very good arguments against that :)
Title: Re: Incredible performace of Lua (luajit) over C++
Post by: DarkRoku12 on May 26, 2015, 08:00:57 pm
i use memory management because i like C style.
If you have some time, you could read the RAII article I linked to, there are very good arguments against that :)
[/quote]

For c++ it should be better using the tools provied the official libraries. But i still thinking the syntax is horrible.

I like a lot more C over C++ ( but sometimes i miss C++ features ).

But it i like C over C++ for this.
1) Is a lot more portable.
2) Less API it have..less you need to know.
3) Its faster ( OpenGL is faster that DirectX ) ( chipmunk-physics are faster than Box2d) ( I use box2d because have a better API and documentation ).

I will see carefully of RAII , looks ougly but great for the memory leaks.

Title: Re: Incredible performace of Lua (luajit) over C++
Post by: Nexus on May 26, 2015, 08:07:24 pm
I like a lot more C over C++.
Nice, but you're making a C++ comparison here. Personal preferences should not affect an objective and expressive benchmark ;)

3) Its faster ( OpenGL is faster that DirectX ) ( chipmunk-physics are faster than Box2d)
Sorry, but this is complete bullshit, especially with those examples. It's so fundamentally wrong I don't even know where to start. But you'll find these kinds of arguments all over the internet, there's no need to repeat them here... So I'd say we concentrate on the benchmark in this thread, no need for another C vs C++ flamewar.

I will see carefully of RAII , looks ougly but great for the memory leaks.
You should really read my article. Exactly this kind of unfunded myths are refuted.

To be honest, you're just giving me more and more the impression that you don't really know what you're talking about. No offense, but when you're trying to prove something (especially speed), you should be familiar with the programming language and measuring techniques -- or at least willing to incorporate improvement suggestions, rather than sticking to dogmas you've heard somewhere and never questioned.

We're still missing the Lua code, by the way :)
Title: Re: Incredible performace of Lua (luajit) over C++
Post by: DarkRoku12 on May 26, 2015, 08:26:13 pm

3) Its faster ( OpenGL is faster that DirectX ) ( chipmunk-physics are faster than Box2d)
Sorry, but this is complete bullshit, especially with those examples. It's so fundamentally wrong I don't even know where to start. But you'll find these kinds of arguments all over the internet, there's no need to repeat them here... So I'd say we concentrate on the benchmark in this thread, no need for another C vs C++ flamewar.

I will see carefully of RAII , looks ougly but great for the memory leaks.
You should really read my article. Exactly this kind of unfunded myths are refuted.


I know that Lua is slower than C++ . When i made the first test i thought  "C++ sure achieve 20 fps or more"..but not.

Luajit is a good tool too, speed up the lua code. ( I'm traying to make the engine comatible with lua 5.2 ...is a headache cause the incompatibilities but it's possible. )

I can post the lua Code.. but there is a lot of libs ( .hpp / .lua ) involved , i can't post all. When i release my engine all will be on github.

You really want to see all the code ? ( i can upload it now if you want )
Title: Re: Incredible performace of Lua (luajit) over C++
Post by: Nexus on May 26, 2015, 08:34:27 pm
The problem is, without code your benchmark doesn't really express anything (we don't know exactly what you did and how you did it). Furthermore, it can't be repeated by others. In short, it's no proof.

But you don't need to publish all files now, you can also wait until your engine is finished, when it's easier to put the benchmark together :)
Title: Re: Incredible performace of Lua (luajit) over C++
Post by: DarkRoku12 on May 26, 2015, 08:59:45 pm
The problem is, without code your benchmark doesn't really express anything (we don't know exactly what you did and how you did it). Furthermore, it can't be repeated by others. In short, it's no proof.

But you don't need to publish all files now, you can also wait until your engine is finished, when it's easier to put the benchmark together :)

Great, i have implemented RAII in the past..but i have a obsessed to know ALL , how it's work, why?, why it was created? I learn some assembly for understand better the low-levels concepts. But more deeper you get in C++ more you have too learn and it's difficult to see all the source files of c++, things that i do because i like programming. But i have social live too..   ::)

Title: Re: Incredible performace of Lua (luajit) over C++
Post by: Tank on May 27, 2015, 08:43:00 am
JIT-compiled programs are known for having optimized execution paths, and thus perform better -- often, not always. We really need the source of both test cases in order to reproduce your performance claims.

What did you use to build the C++ benchmark? Tried Clang?
Title: Re: Incredible performace of Lua (luajit) over C++
Post by: DarkRoku12 on May 27, 2015, 03:28:19 pm
What did you use to build the C++ benchmark? Tried Clang?

No ,i haven't tried.
Is better than GCC ? .

I use the GCC alongside code::blocks ( its the 4.7.1 i think ) , i also use the SFML 2.2 ( SJLJ ) version if my memory don't fail.

But in both case the rendering work is done by the c++ , lua only store the pointers and then pass it to the main loop for the rendering/drawing.

In my engine the "hard" and "dirty" part is done by c++,  lua give c++ the pointers and c++ give the events from the pollevent to lua, and then lua handle it.
Title: Re: Incredible performace of Lua (luajit) over C++
Post by: Grimshaw on May 27, 2015, 06:49:00 pm
Doubt this comparison is fair.. as Nexus said, there are many variables to take into account when making a fair test of runtime speed, so I doubt this one is not distorted..

Though, I have heard really good things about luajit, so I am sure it is really fast nevertheless, given that even the lua default vm is quite efficient.
Title: Re: Incredible performace of Lua (luajit) over C++
Post by: Klaim on May 29, 2015, 10:24:43 pm
Well if fraps tells you that you got low fps, it's certainly that there is a problem with your gpu-side work. In particular when you're doing nothing else than display.
Which means it have nothing to do with C++ or lua....
Title: Re: Incredible performace of Lua (luajit) over C++
Post by: DarkRoku12 on May 30, 2015, 04:45:44 am
Well if fraps tells you that you got low fps, it's certainly that there is a problem with your gpu-side work. In particular when you're doing nothing else than display.
Which means it have nothing to do with C++ or lua....

This are not low fps because there are displaying 1000 crates (512x512) each one.

My pc have i7 4th generation and a max CPU frequency of 3 ghz.

With a regular game i get (with no frame limit)  1400-1700.
Title: Re: Incredible performace of Lua (luajit) over C++
Post by: Rhimlock on June 01, 2015, 09:40:16 am
Am I missing something?
Where is the Lua-code?
How should someone be able to compare your implementations, when you just post the C++ code and say "Look, my Lua Game Engine is faster than C++/SFML"?
Title: Re: Incredible performace of Lua (luajit) over C++
Post by: Nexus on June 01, 2015, 10:06:08 am
Am I missing something?
Obviously several posts :P

Where is the Lua code? How do we know it's equivalent?
The problem is, without code your benchmark doesn't really express anything (we don't know exactly what you did and how you did it). Furthermore, it can't be repeated by others. In short, it's no proof.
We really need the source of both test cases in order to reproduce your performance claims.
Title: Re: Incredible performace of Lua (luajit) over C++
Post by: Rhimlock on June 01, 2015, 10:13:02 am
Obviously several posts :P

I saw your posts about the source but never saw a reply from DarkRoku where he posted the source.
So I thought maybe i overlooked some detail.

At this point, I could just make a Lua game engine that is slower than his C++ implementation to disprove his hypothesis.  ;D

Title: Re: Incredible performace of Lua (luajit) over C++
Post by: SLC on June 02, 2015, 04:57:28 pm
From what I could gather a while back. LuaJit uses SIMD instructions if allowed (by default) and available on the CPU. But you seem to miss the point. In almost every line of your code, you'll end up calling functions. And sometimes (many times) you'll end up calling functions from C/C++ (hopefully through FFI if you want to keep the speed otherwise it's useless with generic binding). And C/C++ will kill LuaJit when it comes to function calls.

Yes, LuaJit could outperform C/C++ when it comes to number crunching, if you have a really modern CPU and you allowed the use of SIMD instructions (will choose the best available instructions). But I highly doubt your code will be only numeric expressions.

Create two serious applications with a few thousands lines of code and not just a simple generic function. And then run them again. You could also disable SIMD instructions in LuaJit if you really want to have a one on one benchmark.

Not to mention that when you start using FFI in LuaJit to maintain that speed. All your safety goes out the window. You might think that C/C++ is an unsafe language. Just wait until you deal with that.
Title: Re: Incredible performace of Lua (luajit) over C++
Post by: Jesper Juhl on June 04, 2015, 09:08:17 pm
Yes, LuaJit could outperform C/C++ when it comes to number crunching, if you have a really modern CPU and you allowed the use of SIMD instructions (will choose the best available instructions)
Modern C++ compilers will also take advantage of SIMD instructions as well (if you let them optimize for modern CPUs) and are actually quite aggressive about vectorizing loops and other code constructs.

Some links to add substance to the above:
http://llvm.org/docs/Vectorizers.html
https://gcc.gnu.org/projects/tree-ssa/vectorization.html
https://msdn.microsoft.com/en-us/library/hh872235.aspx
Title: Re: Incredible performace of Lua (luajit) over C++
Post by: SLC on June 16, 2015, 03:11:37 am
Modern C++ compilers will also take advantage of SIMD instructions as well (if you let them optimize for modern CPUs) and are actually quite aggressive about vectorizing loops and other code constructs.

I know that. However, once you've compiled C++ to use SIMD you can't run your program on a CPU that doesn't feature those instructions. With Lua on the other hand, this is decided at runtime and uses it only if the CPU supports that feature. Therefore your LuaJit program can run both on an old CPU and use only the available instructions and on a newer CPU and use newer instructions without having two binaries.

But that wasn't the point of my post. I simply suggested that LuaJit might have optimized the resulted instructions while C++ didn't (probably wasn't allowed). Which means that the result of the benchmark is almost irrelevant.
Title: Re: Incredible performace of Lua (luajit) over C++
Post by: FRex on June 16, 2015, 11:11:24 am
Quote
I know that. However, once you've compiled C++ to use SIMD you can't run your program on a CPU that doesn't feature those instructions.
Good luck finding a worthwhile x86(-64) desktop CPU with no SSE2 that is younger than 5 years old and faster than 2 Ghz. I'm not even sure one like that exists.

Quote
I simply suggested that LuaJit might have optimized the resulted instructions while C++ didn't (probably wasn't allowed).
This is not true, it's actually the other way around: statically typed and compiled languages are allowed to make long running, drastic optimizations that dynamic languages can't.
Title: Re: Incredible performace of Lua (luajit) over C++
Post by: Daid on July 11, 2015, 01:36:18 pm
Quote
I know that. However, once you've compiled C++ to use SIMD you can't run your program on a CPU that doesn't feature those instructions.
Good luck finding a worthwhile x86(-64) desktop CPU with no SSE2 that is younger than 5 years old and faster than 2 Ghz. I'm not even sure one like that exists.
Been using SSE2 compile flags on a project for 3 years now. More then 50.000 daily users (and growing) not a single complain ever.



But the post here has little to do with C++ vs LuaJit. As the performance of that code should not be CPU bound. Most likely the C++ version is doing vsync while the LuaJit version is not or something silly like that.
Or, more likely, you say you have the physics engine enabled on the LuaJit version, making crates fall off-screen. Off-screen rendering is quite fast, as the GPU will say "I don't have to do shit, let's skip this".
Title: Re: Incredible performace of Lua (luajit) over C++
Post by: Hydra on August 18, 2015, 07:41:11 pm
I like a lot more C over C++.
Nice, but you're making a C++ comparison here. Personal preferences should not affect an objective and expressive benchmark ;)

3) Its faster ( OpenGL is faster that DirectX ) ( chipmunk-physics are faster than Box2d)
Sorry, but this is complete bullshit, especially with those examples. It's so fundamentally wrong I don't even know where to start. But you'll find these kinds of arguments all over the internet, there's no need to repeat them here... So I'd say we concentrate on the benchmark in this thread, no need for another C vs C++ flamewar.

I will see carefully of RAII , looks ougly but great for the memory leaks.
You should really read my article. Exactly this kind of unfunded myths are refuted.

To be honest, you're just giving me more and more the impression that you don't really know what you're talking about. No offense, but when you're trying to prove something (especially speed), you should be familiar with the programming language and measuring techniques -- or at least willing to incorporate improvement suggestions, rather than sticking to dogmas you've heard somewhere and never questioned.

We're still missing the Lua code, by the way :)

He just uploaded a benchmark of Lua code and C++ code. Seeing as you guys generally help people there's no need to be so rude to him:

"this is complete bullshit!"
"It's so fundamentally wrong I don't even know where to start"
"giving me more and more the impression that you don't really know what you're talking about"
"rather than sticking to dogmas you've heard somewhere and never questioned."

All he's done is uploaded a benchmark of his Lua engine vs SFML C++ engine and you suddenly go all defensive and start being rude to him. Perhap's you shouldn't even reply to comments seeing as when viewing your posts the first one includes a quote of someone saying your being offensive.
Title: Re: Incredible performace of Lua (luajit) over C++
Post by: Tank on August 19, 2015, 10:12:44 am
Disclaimer, as I also replied and thus feel addressed: I was not being rude! :D I really just wanted to see the Lua code. I even think that it's possible that his tests outperform C++. JIT compilation is quite powerful, as it can optimize in a field where compile-time optimizers have already left the building.
Title: Re: Incredible performace of Lua (luajit) over C++
Post by: Hydra on August 19, 2015, 02:54:22 pm
Disclaimer, as I also replied and thus feel addressed: I was not being rude! :D I really just wanted to see the Lua code. I even think that it's possible that his tests outperform C++. JIT compilation is quite powerful, as it can optimize in a field where compile-time optimizers have already left the building.

I was only mainly talking about Nexus and his first comment which was completely unprovoked and just rude.
Title: Re: Incredible performace of Lua (luajit) over C++
Post by: Nexus on August 19, 2015, 03:21:00 pm
You take a 3 months old post out of context, denying any backgrounds leading to it, for... what? If you have a problem with my attitude, write me a PM, there's no need to necromance this thread for an off-topic discussion.
Title: Re: Incredible performace of Lua (luajit) over C++
Post by: Hydra on August 19, 2015, 03:59:47 pm
You take a 3 months old post out of context, denying any backgrounds leading to it, for... what? If you have a problem with my attitude, write me a PM, there's no need to necromance this thread for an off-topic discussion.

I didn't purposely deny the lead up I was just quoting the parts of it that were rude. For people who didn't want to read the whole post which I quoted.