C + + vs C #: Performance

Source: Internet
Author: User

Http://creamysoft.blogspot.hk/2013/05/c-vs-c-performance.html

C + + C: Performance Most programmers know this performance isn ' t the most important consideration when picking a Langua  Ge.  Ease of use and community support is also important. Typically it's wise to pick the highest-level language so can meet the demands of the application, to use the right tool For the job.
 my job is game development.  more and more games is being written completely in higher-level languages like C # and Java, which certainly have th Eir advantages. last Year, I is working on a C # game engine, because I like C # and because it ' s good enough for a lo T of games.  but I was curious on just how much slower it's for a game engine than C + +.  so I put together a simple c#-only demo (using OPENTK) that draws a bunch of 3D textured cubes and bounces them arou nd inside the view volume.  each Cube is drawn and animated every frame.  i also implemented dynamic batching whereby individual cubes is combined into 1 draw call.  then I pumped up the number of cubes until the frame rate got down to ~30 fps (~34 ms per frame).  i ended up WI Th 25600 cubes being drawn and moved each frame.  my PC is starting-to-age, but I had a Core i7 processor with 8 cores (but the test app was a single threaded), GB o F RAM, and an nVidia GeForce 8600GTS video Card. then I wrote a C + + version of the test app using OpenGL and Freeglut.  it did exactly the same thing:the same geometry, the same texture, the same algorithm for bouncing, the same number of cubes.  it looked identical in every-out, and It ran more than twice as fast (~13 ms per frame).  if Google C # C + + Performance comparison ", you'll get a host of articles comparing performance between the and other languages, like Jav A.  i found quite a few claiming that C # is only 10-20% slower than C + +, which IMO, makes C # the clear choice.  but there is others that say C # is only half the speed of C + +, which is what I found  for the case .  to be clear, C # isn ' t so much slower for every case, and it's perfectly suited to a wide array of applications, incl Uding many games. so after seeing the difference between pure C # and Pure C + +, I was curious how a hybrid would perfo Rm.  i wrote the core app in C + + which embedded the Mono runtime and performed all the batching and rendering.  the C + + code loaded a C # assembly which instantiated the cubes and performed the "game logic" of bouncing the cubes Around.  i wanted to emulate the the-game engines like Unity use mono:c++ engine + game logic in C #.  again, this hybrid test app produced exactly the same output as the C # and C + + versions, and it ran just a little mo Re slowly than the pure C + + version (~15 ms per frame).  the code for all 3 apps is here.  i built and ran these apps on Linux, and so I can ' t vouch for Windows.  i used Eclipse for the C + + version and QT Creator for the hybrid.  i used Mono Develop for the C # CoDe. a Couple Other Notes:
    • The C # code does not perform any memory allocations during the game loop, so garbage collection isn ' t an issue at all.
    • The hybrid version uses unsafe code in order for the C # and C + + sides to share the same memory. Specifically, the C # code updates the positions and velocities of the cube in the same memory that the C + + code then reads  When it renders the cubes. I don ' t know how Unity does it, but I would imagine that marshaling that data across the c#/c++ boundary would only make I  T slower. The code in the zip file shows what I did it.
Conclusion  So do I continue with my C # engine?  nope.  it is a close call, but I thought that the types of games that I want to make might cause a pure C # game to Bog Dow N (like Minecraft does (which are an awesome game ... I ' m not ragging on it).  i ran into this situation with now Boarding.  it performed great until we started getting closer to the end after adding more and more visual content.  since i ' m starting a business now, laying a new foundation for the future work, I don ' t want to get stuck later with a C Ode base I can ' t make fast enough.  so I decided to lay C # aside and pick C + + back up again. what about the hybrid version?  isn ' t it the best of both worlds?  yes and No.  mono is a fast, extremely functional runtime, and it's pretty easy-to-embed in applications.  but It's a giant runtime with all kinds of features I don ' t need.  it ' s also not completely free.  integrating a scripting language with an engine are a lot of woRK (i ' ve do it with LUA, and I started with mono), and in the end I decided to stick with pure c++. there is a cou Ple of questions that naturally follow:
    • ISN ' t The productivity of C # worth the runtime cost?
    • Why would you write your own engine anyway?
I ' ll write about those separately.

C + + vs C #: Performance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.