PDA

View Full Version : C vs. C++ vs. C#


Lucky
06-22-2012, 09:48 PM
Hi. I am a computer scientist. This is a comparison of memory usage for a simple hello world application between C, C++, and C#. Compiled VS2010 32-bit x86 Release.

// C#
// 1,968k
using System;

namespace hellonet
{
class Program
{
static void Main(string[] args)
{
System.Console.Write("hello world");
System.Console.ReadKey();
}
}
}

// C++
// 640k
#include <iostream>

int main(int argc, char* argv[])
{
std::cout << "hello world";
std::cin.get();
return 0;
}

// C
// 484k
#include <stdio.h>

int main(int argc, char* argv[])
{
printf("hello world");
getchar();
return 0;
}

As you can see, C# uses 5x as much memory as C and 3x more than C++ just for a simple hello world application. And M$ is writing Windows.Next kernel in this managed language.

Rogean you can thank me for the adsense money from the future googlers that will find this site (as applies to all of my insights made here).

Lucky
06-22-2012, 09:59 PM
Note: I was using the MS C compiler for the C code. Bjarne Strassoup (creator of C++) said that hello world should generate literally same code. So I copied the C code and compiled with MS C++ compiler and it was 488k.

interdasting

Razdeline
06-22-2012, 10:09 PM
c++ for obv~ reasons

Lubian
06-22-2012, 11:39 PM
I'm kind of wondering why you're comparing the memory usage of each of these Hello World programs in the different languages; is that supposed to be a way to determine which language is better? God, I hope not.

Furthermore, what also saddens me is your leap from this into the Windows 8 Kernel and managed, but I have a feeling you don't know what managed means.

Also, since you used "using System;" so you don't have to write the namespace System before Console. ReadKey() probably doesn't do what you think it does; I think your intention would be Read().

Lastly, you don't have to wonder if they generate the same code, since you can view the disassembly very easily in VS2010, but ensure you're viewing the release build when you do so.

Lucky
06-22-2012, 11:50 PM
Of course I knew there would be criticism of comparing hello world applications, but I sincerely doubt C# will perform any better when dealing with generic containers compared to C++ STL which inlines to C.

Perhaps it is unfair as .NET is a framework, so I will compare it to Qt.

// Qt C++
// 960k
#include <QtCore/QCoreApplication>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

std::cout << "hello world";
std::cin.get();

return a.exec();
}


I didn't say anything about Windows 8.

Midori is the code name for a managed code operating system being developed by Microsoft Research. It has been reported[1][2] to be a possible commercial implementation of the Singularity operating system, a research project started in 2003 to build a highly-dependable operating system in which the kernel, device drivers, and applications are all written in managed code. Microsoft has mapped out several possible migration paths from Windows to Midori (referred to as "Windows.Next").[5][6][7]

The namespace was unnecessary I'll agree, but not something which would have effected the compiler. From my understanding, ReadKey() is more-o'er-less similar to a single getchar() and std.cin() beyond the fact that return doesn't need to be pressed.

Lubian
06-23-2012, 12:32 AM
Of course I knew there would be criticism of comparing hello world applications, but I sincerely doubt C# will perform any better when dealing with generic containers compared to C++ STL which inlines to C.

Performance wise, probably every developer knows that the lower level C languages are going to be faster than C#. It's also pretty widely known that the simple C# programs use more memory, because they purposely didn't design for an empty exe; since if you needed something with a low memory footprint or something time critical you would use a lower level language. This should be of no surprise for anyone who knows what a high level or low level programming language is and the advantages and disadvantages between the two.

The namespace was unnecessary I'll agree, but not something which would have effected the compiler. From my understanding, ReadKey() is more-o'er-less similar to a single getchar() and std.cin() beyond the fact that return doesn't need to be pressed.

I'm starting to think it's a waste of time even responding to you. You can clearly see the return type of ReadKey() from the IDE (it's a struct). I don't know why you would continue spewing misinformation after someone told you it might have been wrong, and the fact that it takes 1 second to validate.

I'll stop wasting my time here. Good luck with your thread and checking how much memory Hello World programs use in different languages.

Spectre
06-23-2012, 01:01 AM
I could write a C++ program that could kill your whole family.

Lucky
06-23-2012, 01:55 AM
As far as ReadKey() goes, it is the accepted practice of preventing a console application of immediately closing. I tried Read(), and of course there wasn't any significant difference in the memory footprint. Maybe they load similar buffers, or don't load any until the key is actually pressed and the method returns, that I do not know.

What inspired this in the 1st place was reading this statement on stackoverflow: The .Net framework takes anywhere from 5-15 megabytes of RAM just to load up enough code to execute "Hello World".

Obviously C# was going to fail in this. It's JIT'ed, which means that it's an inefficient waste of crap language designed solely for the purpose of treating ignorant software programmers as fodder for applications developed on a Microsoft platform. I don't care how easy it is to program in; there's no way in hell I'll use a language which doesn't put forth the idea of "flexibility" and "control" as important. C# was designed to make Microsoft money, and to provide businesses the ability to write generic, lame, CRUD applications.

But after seeing all of this I'm reaffirmed and glad I switched to Qt years ago and never looked back.

somnia
06-23-2012, 03:09 AM
Hi. I am a developer. This is the reality check.

Taken from http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf referenced by http://en.wikipedia.org/wiki/C_Sharp_(programming_language)#cite_note-ECMA-334-6.

As the definition of C# evolved, the goals used in its design were as follows:

• C# is intended to be a simple, modern, general-purpose, object-oriented programming language.
• The language, and implementations thereof, should provide support for software engineering principles
such as strong type checking, array bounds checking, detection of attempts to use uninitialized variables,
and automatic garbage collection. Software robustness, durability, and programmer productivity are
important.
• The language is intended for use in developing software components suitable for deployment in
distributed environments.
• Source code portability is very important, as is programmer portability, especially for those
programmers already familiar with C and C++.
• Support for internationalization is very important.
• C# is intended to be suitable for writing applications for both hosted and embedded systems, ranging
from the very large that use sophisticated operating systems, down to the very small having dedicated
functions.
• Although C# applications are intended to be economical with regard to memory and processing power
requirements, the language was not intended to compete directly on performance and size with C or
assembly language.

I've bolded the most important bullet point for your reference. I understand many of you machine-hybrid basement dwellers enjoy ultra efficiency as I employ a number of you for highly specialized tasks in my company but keep in mind that it is important to use the right tool for the job.

Lucky
06-23-2012, 03:17 AM
hence why it sux

somnia
06-23-2012, 03:19 AM
hence why you prob work for people like me ;)

Sodapop
06-23-2012, 03:21 AM
i wish i was an engineering major taking cs1 maybe 2. jesus christ.

Sodapop
06-23-2012, 03:22 AM
I could write a C++ program that could kill your whole family.

make that monitor explode.

Lucky
06-23-2012, 03:23 AM
hence why you prob work for people like me ;)

No I'm a good American and outsource to india like everyone else

Lucky
06-23-2012, 03:28 AM
singlehandedly proved beyond a doubt how badly C# is a pile of shit and microsoft does not give a fuck anymore, analogous to the disaster in performance we saw from win2k to vista, and nerd patrol making 42.0k/yr to write shitty 1shot applications for the local bank thinkin theyre bill gates with millions of slaves below them get all mad

somnia
06-23-2012, 03:35 AM
wow u mad

Lucky
06-23-2012, 03:38 AM
This why I don't do nice things for u ppl

int __thiscall sub_100021C0(void *this, int a2, int a3)
{
void *v3; // edi@1
int result; // eax@2
char v5; // [sp+8h] [bp-804h]@1
char v6; // [sp+9h] [bp-803h]@1
unsigned int v7; // [sp+808h] [bp-4h]@1
int v8; // [sp+80Ch] [bp+0h]@1

v7 = (unsigned int)&v8 ^ dword_100871B4;
v3 = this;
v5 = 0;
sub_100075B0(&v6, 0, 2047);
if ( *(_BYTE *)a3 )
{
sub_10001D40(&v5, a3, 1, 0, 0, 0, 0, 0);
result = sub_1001F430(&v5, "/rogean");
if ( !result )
return result;
sub_1001F430(&v5, "/rogtest");
}
return sub_10001EA0(v3);
}

Sodapop
06-23-2012, 03:42 AM
http://www.accelereyes.com/content/images/VectorizedLoop1.jpg

Lucky
06-23-2012, 03:49 AM
push 0
lea eax, [ebp+var_8]
push eax
mov ecx, offset aName_4 ; "Name"
mov edx, [ebp+var_30]
mov eax, ebx
mov edi, [eax]
call dword ptr [edi]
push 0
lea eax, [ebp+var_C]
push eax
mov ecx, offset aPath_5 ; "Path"
mov edx, [ebp+var_30]
mov eax, ebx
mov edi, [eax]
call dword ptr [edi]
push 0
lea eax, [ebp+var_10]
push eax
mov ecx, offset aBinary_4 ; "Binary"
mov edx, [ebp+var_30]
mov eax, ebx
mov edi, [eax]
call dword ptr [edi]
push 0
lea eax, [ebp+var_14]
push eax
mov ecx, offset aFlag_0 ; "Flag"
mov edx, [ebp+var_30]
mov eax, ebx
mov edi, [eax]
call dword ptr [edi]
push 0
lea eax, [ebp+var_18]
push eax
mov ecx, offset aStartup_5 ; "Startup"
mov edx, [ebp+var_30]
mov eax, ebx
mov edi, [eax]
call dword ptr [edi]
push 0
lea eax, [ebp+var_1C]
push eax
mov ecx, offset aIcon_1 ; "Icon"
mov edx, [ebp+var_30]
mov eax, ebx
mov edi, [eax]
call dword ptr [edi]
push 0
lea eax, [ebp+var_20]
push eax
mov ecx, offset aTime_2 ; "Time"
mov edx, [ebp+var_30]
mov eax, ebx
mov edi, [eax]
call dword ptr [edi]
push offset dword_4C9AE0
lea eax, [ebp+var_28]
push eax
mov ecx, offset aUsefreq_2 ; "UseFreq"
mov edx, [ebp+var_30]
mov eax, ebx
mov edi, [eax]
call dword ptr [edi]
push 0
mov ecx, offset aRemovegame_3 ; "RemoveGame"
mov edx, [ebp+var_30]
mov eax, ebx
mov edi, [eax]
call dword ptr [edi+10h]
test al, al
jz short loc_4C9902

Sodapop
06-23-2012, 03:52 AM
i didn't copy paste mine i didn't want someone destroying their p99 world.

Lucky
06-23-2012, 03:55 AM
http://pastebin.com/aw3vtWnr

line 27 is where the magic happens

somnia
06-23-2012, 12:39 PM
Wow u really mad to post code on p1999 boards. Did you recently get laid off and replaced by a fresh out-of-college hot shot? Nobody is disputing the "efficiency" - it's that most people given the choice don't want to look at or write assembly code if it is not necessary. Simple tasks become unnecessarily many lines of code and getting others to rapidly understand your code is more difficult.

The efficiency cost of the abstraction of higher level languages is traded for portability and human-readability. Again, remember it's about using the right tool for the right job - system software is an obvious arena for lower level languages. On the other hand, rapid application development that must run on multiple platforms is best developed using languages like C# or Java.

Did you get laid off by Microsoft?

Lucky
06-23-2012, 01:13 PM
Wow u really mad to post code on p1999 boards. Did you recently get laid off and replaced by a fresh out-of-college hot shot? Nobody is disputing the "efficiency" - it's that most people given the choice don't want to look at or write assembly code if it is not necessary. Simple tasks become unnecessarily many lines of code and getting others to rapidly understand your code is more difficult.

The efficiency cost of the abstraction of higher level languages is traded for portability and human-readability. Again, remember it's about using the right tool for the right job - system software is an obvious arena for lower level languages. On the other hand, rapid application development that must run on multiple platforms is best developed using languages like C# or Java.

Did you get laid off by Microsoft?

How that make me mad, unless you mean the kind where I have a smile on my face? School never taught me anything besides a bunch of 4-dimensional multivariable calculus bullshit, I started out at 5 years old doing GW-basic on DOS.

I'm not talking about assembly, I know higher level is and has been the future since Algol and Fortran, which is why I compared .Net to Qt. Qt arguably has more tools available, better cross platform compatibility, a way better organized library (compare MSDN's half-baked attempt to Qt's documentation), and of course as proven has better resource usage. So why trade all that just to use a shitty M$ product, when you gain no productivity and lose out?

The whole point is they're writing the Windows.Next kernel in this shit! People already complain how Windows is so bad after Vista, then this shit! If you don't think a kernel should be written in C and asm then you're the dummy.

I have never worked for Microsoft, I chose the Linux route wherever possible as M$ products are complete garbage. The only exception to that may be Visual Studio (when Intellisense doesn't bug out, drop a point in for Qt Creator); one of my early mentors is a seniour developer of it.

somnia
06-23-2012, 01:29 PM
Why do you care what Microsoft writes their shit in then? If they really don't give a shit then sooner or later it will show. Internet Explorer is a shit product too and users are rapidly leaving it for newer and better browsers.

Microsoft is making this decision for a reason....albeit a seemingly obvious bad one. It might have to do with the market's transition to a more diverse market of mobile devices. See http://en.wikipedia.org/wiki/Microsoft_Silverlight.

Lucky
06-23-2012, 01:46 PM
Silverlight is pretty cool, as is all of WPF. One thing I remember in school was them drilling time vs. space tradeoffs into our head, so I'm pretty sure its a lil important, and with .NET you just seem to lose on both.

I just don't get why people allow Microsoft to trump the market, especially when they stopped giving a shit about performance anymore. Its like that for most things in life though, people just dum. But srsly for a minute, think how many schools in America still rock computers less than 512mb. I bet in the country and the ghetto it's very common. Installing Windows 7's bloated ass (let alone Vista, 8, or this .Next shit) on all these computers is just ludicrous! We should be putting something like Ubuntu in the classroom and migrate the next generation to Linux already.

impact
06-24-2012, 05:18 PM
If they really don't give a shit then sooner or later it will show.

Um, MS hasn't given a shit about quality software since they started, and it has been painfully obvious to anyone who can tell quality software from poor software since the beginning. I couldn't agree with Lucky more on this. The fact that MS software is so prevalent today is absolute insanity and, IMHO, indicative of how dumbed down our society is. No one who writes code denies that using low level languages for all application development is far too laborious, but Lucky's original point that MS software sucks ass stands and is correct. It should be boycotted and replaced with something that actually has quality.

My two cents.

sawin
06-24-2012, 11:41 PM
i dont know very much about c#, but why does the last line of code read input? it seems unnecessary and you could just as easily read for input in the other 2 programs. is it because youre attempting to get a higher number?

incidentally, i have a toon named fgets

Lucky
06-24-2012, 11:53 PM
The other ones read for input too.

# python
# 3,772k
print "hello world"
raw_input()

Lucky
06-24-2012, 11:54 PM
py2.7*

Nirgon
06-25-2012, 02:56 AM
Lol @ "most people don't write assembly"

The. Fuck.

Is. This.

Going.

sawin
06-26-2012, 03:25 AM
why does the last line of code read input?

i dunno why i didnt notice the first time that the others read input, but still...why read for input? reading for input is not part of a standard HelloWorld afaik. is it because youre attempting to achieve a higher number on c#?

Lucky
06-26-2012, 03:38 AM
I just did that because I am lazy and it was easy to profile it before it returned to the operating system, so it didn't exit instantly. Could have used debugger, but maybe is better since it has both I/O.

doraf
06-27-2012, 06:33 PM
The whole point is they're writing the Windows.Next kernel in this shit! People already complain how Windows is so bad after Vista, then this shit! If you don't think a kernel should be written in C and asm then you're the dummy.


Where are you getting your information? Are you talking about WinRT? RT code is native.

Lucky
06-27-2012, 08:27 PM
Midori is the code name for a managed code operating system being developed by Microsoft Research. It has been reported[1][2] to be a possible commercial implementation of the Singularity operating system, a research project started in 2003 to build a highly-dependable operating system in which the kernel, device drivers, and applications are all written in managed code. Microsoft has mapped out several possible migration paths from Windows to Midori (referred to as "Windows.Next").[5][6][7]

http://en.wikipedia.org/wiki/Midori_(operating_system)

doraf
06-28-2012, 11:50 PM
Midori is the code name for a managed code operating system being developed by Microsoft Research. It has been reported[1][2] to be a possible commercial implementation of the Singularity operating system, a research project started in 2003 to build a highly-dependable operating system in which the kernel, device drivers, and applications are all written in managed code. Microsoft has mapped out several possible migration paths from Windows to Midori (referred to as "Windows.Next").[5][6][7]

http://en.wikipedia.org/wiki/Midori_(operating_system)

The theory of a managed code OS really isn't a bad idea since there are no real pointers, but this is not the Win 8 kernal, so you don't need to worry. The basic anology is, everyone knows sticking a knife into a lightsocket is bad. A managed code OS would prevent the knife from entering the light socket.

Here's a decent discussion for the MFC programmers regarding WinRT.
http://social.msdn.microsoft.com/Forums/en-US/vcmfcatl/thread/3d1d68ff-21fd-4770-a0f8-288a9a92b6c9

Lucky
06-29-2012, 01:20 AM
is all bad dog, w/e man u dum

Lucky
06-29-2012, 11:17 AM
sry 4 callin u dum

norova
06-29-2012, 11:57 AM
Just to be a nitpicky a-hole, the C# code uses 4x the memory of the C code, not 5x. This tiny error may someday cause the world to end, so I thought I'd point it out.