C++ vs C#: Performance

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

C++ vs C#: Performance

Most programmers know that performance isn‘t the most important consideration when picking a language.  Ease of use and community support are also important.  Typically it‘s wise to pick the highest-level language that can meet the demands of the application, to use the right tool for the job.

My job is game development.  More and more games are being written completely in higher-level languages like C# and Java, which certainly have their advantages.

Last year, I was working on a C# game engine, because I like C# and because it‘s good enough for a lot of games.  But I was curious about just how much slower it is 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 around inside the view volume.  Each cube is drawn and animated every frame.  I also implemented dynamic batching whereby individual cubes are 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 with 25600 cubes being drawn and moved each frame.  My PC is starting to age, but I have a Core i7 processor with 8 cores (but the test app is single threaded), 12 GB of RAM, and an nVidia GeForce 8600 GTS 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 way, and it ran more than twice as fast (~13 ms per frame).

If you google "c# c++ performance comparison", you‘ll get a host of articles comparing performance between the two and other languages, like Java.  I found quite a few claiming that C# is only 10-20% slower than C++, which IMO, makes C# the clear choice.  But there are others that say C# is only half the speed of C++, which is what I found for this case.  To be clear, C# isn‘t so much slower for every case, and it‘s perfectly suited to a wide array of applications, including many games.

So after seeing the difference between pure C# and pure C++, I was curious how a hybrid would perform.  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 way 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 more 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 it slower.  The code in the zip file shows how I did it.

Conclusion

 

So did I continue with my C# engine?  Nope.  It was a close call, but I thought that the types of games that I want to make might cause a pure C# game to bog down (like Minecraft does (which is 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 future work, I don‘t want to get stuck later with a code 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 is 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 is a lot of work (I‘ve done it with lua, and I started with mono), and in the end I decided to stick with pure C++.

There are a couple 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.

时间: 2024-10-18 14:51:41

C++ vs C#: Performance的相关文章

Performance Monitor4:监控SQL Server的IO性能

SQL Server的IO性能受到物理Disk的IO延迟和SQL Server内部执行的IO操作的影响.在监控Disk性能时,最主要的度量值(metric)是IO延迟,IO延迟是指从Application创建IO请求,到Disk完成IO请求的时间延迟.如果物理Disk不能及时完成IO请求,跟不上请求负载的速度,那么SQL Server就容易出现性能问题.SQL Server内部在执行一些特定的操作时,会和Disk做读写交互,这也会影响物理硬盘响应SQL Server的IO请求的性能,使查询进程处

Performance Monitor3:监控SQL Server的内存压力

SQL Server 使用的资源受到操作系统的调度,同时,SQL Server在内部实现了一套调度算法,用于管理从操作系统获取的资源,主要是对内存和CPU资源的调度.一个好的数据库系统,必定在内存中缓存足够多的信息,以减少从物理硬盘中读取数据的次数:如果内存是系统瓶颈,那么SQL Server一定会运行的非常慢.监控SQL Server的内存压力,需要从Widnows级别上,对内存使用的整体使用情况进行监控:从SQL Server级别上,监控SQL Server对内存资源的使用情况. 一,从Wi

Performance Monitor Usage5:View Report

1,当Data Collector Set 停止运行后,在Reports Catalog->User Defined->DataCollectorSet_PhysicalDisk 下,会有Data Collection文件,点击View->Performance Monitor,就能查看DataCollectorSet_PhysicalDisk 统计的Performance Counter 数据. 2,这实际上是一个log file,点击View Log Data 能够看到Data So

Improving performance – A full stack problem

Improving performance – A full stack problem March 6, 2015 by ronald 4 Comments Improving the performance of a web system involves knowledge of how the entire technology stack operates and interacts. There are many simple and common tips that can pro

linux Performance tools

废话不说首先看图,这是摘自以为大神的ppt,原文在这里 http://www.brendangregg.com/Slides/SCaLE_Linux_Performance2013.pdf 准备把上面列举出来的图片挨个写blog 分享一下,哦也 linux Performance tools,码迷,mamicode.com

Microsoft.SQL.Server2012.Performance.Tuning.Cookbook学习笔记(一)

一.Creating a trace or workload 注意点: In the Trace Properties dialog box, there is a checkbox option in the General tab with the caption Server processes trace data, to specify whethertrace data should be processed on the server. If not checked, trace

Fulltext Index Study4:management and performance

Only one full-text index is allowed per table. For a full-text index to be created on a table, the table must have a single, unique nonnull column. You can build a full-text index on columns of type char, varchar, nchar, nvarchar, text, ntext, image,

Performance Monitor Usage6:Side Effect

开启Performance Monitor 会不会对Server 性能有影响? 在一个Production Server上,运行一段时间之后,在Task Management 中长款Performance Monitor消耗的资源 引用<SQL Server 2012 实施与管理实战指南>来解释我的疑惑: 默认的Performance Monitor 是用来Real-Time 检测系统的,在现实的问题分析中,更多的是需要在问题发生之前,开启Performance Counter的数据收集,一段

超级简单实用的.NET性能调试工具-----ANTS Performance Profiler

艺术字做到后期,性能成了一个比较大的瓶颈,优化性能的过程中接触了一些.NET性能调试工具,最喜欢的是ANTS Performance Profiler,操作简单,功能也跟我期望的差不多. ANTS Performance Profiler是出自大名鼎鼎的Red Gate,很多人使用过的.NET Reflector就是出自此公司,该公司还出品其他产品,具体请戳http://www.red-gate.com/products/.ANTS Performance Profiler支持.NET平台所有语

[转]Disk Space Usage and SQL Server Performance

Disk Space Usage and SQL Server Performance By Gregory A. Larsen When you think of SQL Server performance components, you think of the usage of CPU, memory, and the I/O it takes to process a query. However, there is another component you should consi