<p>I want to create a simple http proxy server that does some very basic processing on the http headers (i.e. if header x == y, do z).  The server may need to support hundreds of users.  I can write the server in C# (pretty easy) or c++ (much harder).  However, would a C# version have as good of performance as a C++ version?  If not, would the difference in performance be big enough that it would not make sense to write it in C#?</p>

[{'answer_id': 30755, 'author': 'Brian Ensink', 'author_id': 1254, 'author_profile': 'https://Stackoverflow.com/users/1254', 'pm_score': 0, 'selected': False, 'text': "<p>The proxy server you describe would deal mostly with string data and I think its reasonable to implement in C#.  In your example,</p>\n\n<pre><code>if header x == y, do z\n</code></pre>\n\n<p>the slowest part might actually be doing whatever 'z' is and you'll have to do that work regardless of the language.</p>\n"}, {'answer_id': 30756, 'author': 'Christopher', 'author_id': 3186, 'author_profile': 'https://Stackoverflow.com/users/3186', 'pm_score': 2, 'selected': False, 'text': "<p>Why do you expect a much higher performance from the C++ application?</p>\n\n<p>There is no inherent slowdown added by a C# application when you are doing it right. (not too many dropped references, frequent object creation/dropping per call, etc.)</p>\n\n<p>The only time a C++ application really outperforms an equivalent C# application is when you can do (very) low level operations. E.g. casting raw memory pointers, inline assembler, etc.</p>\n\n<p>The C++ compiler may be better at creating fast code, but mostly this is wasted in most applications. If you do really have a part of your application that must be blindingly fast, try writing a C call for that hot spot.</p>\n\n<p>Only if most of the system behaves too slowly you should consider writing it in C/C++. But there are many pitfalls that may kill your performance in your C++ code.</p>\n\n<p>(TLDR: A C++ expert may create 'faster' code as an C# expert, but a mediocre C++ programmer may create slower code than mediocre C# one)</p>\n"}, {'answer_id': 30757, 'author': 'akmad', 'author_id': 1314, 'author_profile': 'https://Stackoverflow.com/users/1314', 'pm_score': 0, 'selected': False, 'text': "<p>In my experience, the design and implementation has much more to do with performance than do the choice of language/framework (however, the usual caveats apply: eg, don't write a device driver in C# or java).</p>\n\n<p>I wouldn't think twice about writing the type of program you describe in a managed language (be it Java, C#, etc).  These days, the performance gains you get from using a lower level language (in terms of closeness to hardware) is often easily offset by the runtime abilities of a managed environment.  Of course this is coming from a C#/python developer so I'm not exactly unbiased...</p>\n"}, {'answer_id': 30766, 'author': 'chakrit', 'author_id': 3055, 'author_profile': 'https://Stackoverflow.com/users/3055', 'pm_score': 3, 'selected': True, 'text': "<p>You can use <strong>unsafe</strong> C# code and <strong>pointers</strong> in critical bottleneck points to make it run faster. Those behave much like C++ code and I believe it executes <em>as fast</em>.</p>\n\n<p>But most of the time, C# is JIT-ted to uber-fast already, I don't believe there will be much differences as with what everyone has said.</p>\n\n<p>But one thing you might want to consider is: <strong>Managed code (C#) string operations are rather slow compared to using pointers effectively in C++.</strong> There are more optimization tricks with C++ pointers than with CLR strings.</p>\n\n<p>I think I have done some benchmarks before, but can't remember where I've put them.</p>\n"}, {'answer_id': 70898, 'author': '0124816', 'author_id': 11521, 'author_profile': 'https://Stackoverflow.com/users/11521', 'pm_score': 0, 'selected': False, 'text': "<p>If you need a fast and reliable proxy server, it might make sense to try some of those that already exist. But if you have custom features that are required, then you may have to build your own. You may want to collect some more information on the expected load: hundreds of users might be a few requests a minute or a hundred requests a second.</p>\n\n<p>Assuming you need to serve under or around 200 qps on a single machine, C# should easily meet your needs -- even languages known for being slow (e.g. Ruby) can easily pump out a few hundred requests a second.</p>\n\n<p>Aside from performance, there are other reasons to choose C#, e.g. it's much easier to write buffer overflows in C++ than C#.</p>\n"}, {'answer_id': 496493, 'author': 'devdimi', 'author_id': 54983, 'author_profile': 'https://Stackoverflow.com/users/54983', 'pm_score': 1, 'selected': False, 'text': '<p>I would expect the C# version to be nearly as fast as the C++ one but with smaller memory footprint.\nIn some cases managed code is actually a LOT faster and uses less memory compared to non optimized C++. C++ code can be faster if written by expert, but it rarely justifies the effort.</p>\n\n<p>As a side note I can recall a performance "competition" in the blogosphere between Michael Kaplan (c#) and Raymond Chan (C++) to write a program, that does exactly the same thing. Raymond Chan, who is considered one of the best programmers in the world (Joel) succeeded to write faster C++ after a long struggle rewriting most of the code.   </p>\n'}, {'answer_id': 496614, 'author': 'Nemanja Trifunovic', 'author_id': 8899, 'author_profile': 'https://Stackoverflow.com/users/8899', 'pm_score': 0, 'selected': False, 'text': '<p>Is your http server going to run on a dedicated machine? If yes, I would say go with C# if it is easier for you. If you need to run other applications on the same machine, you\'ll need to take into account the memory footprint of your application and the fact that GC will run at "random" times.</p>\n'}]
['https://Stackoverflow.com/questions/30729', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3291/']