Why is C++ faster than [IMPUT LANGUAGE NAME]?

closed account (z1CpDjzh)
all programing languages are turned to 1s and 0s so why would C++ be faster than
[IMPUT LANGUAGE NAME]
?
Last edited on
a) imput isnt a word. i believe you are looking for input. and its not. a languages speed depends on many things. for example, how well the code is written, what is turning it into ones and zereos, how it is being turned into ones and zereos, the optimization of the program, the environment being run on, and the hardware of the computer
C/C++ is closer to the hardware than most other languages, hence it's theoretically faster than those other languages.

C is faster then C++ due to the lack of automatisms like destructor call.

languages like C#, Jave, etc. replaced the destructor call with garbage collection which theoretically reduces that time. They also depend alot on C, so...

Yes the most on how well the code and/or library is written
Ehhhhh. Have to butt in. Too many errors.

coder777 wrote:
C is faster then C++ due to the lack of automatisms like destructor call.


Destructors that do nothing cost nothing. Destructors that do something cannot be skipped, even in C, or you have resource leaks. Cleanup is important.

The only difference between C and C++ with regard to cleanup is that in C++ the code is called automatically whereas in C you have to do it manually. Either way... cleanup code is still being executed, so neither language is "faster" in this area:

1
2
3
4
5
6
7
// C++ code:
std::ifstream foo("myfile");  // ctor opens the file
// dtor closes the file

// C code:
FILE* foo = fopen("myfile","r"); // fopen opens the file
fclose(foo);  // fclose closes the file 


languages like C#, Jave, etc. replaced the destructor call with garbage collection which theoretically reduces that time.


C# and Java still have destructors. GC does not replace them. GC just goes through allocated memory and removes blocks that are no longer being used.

C and C++ free memory as soon as the object is destroyed. GC languages free it in "clumps" when the GC kicks in. So they're both doing the same work, just at different times.



TheGentlmen wrote:
all programing languages are turned to 1s and 0s so why would C++ be faster than
[IMPUT LANGUAGE NAME]
?


The typical answer to this is that C++ is compiled directly into native instructions that the computer can execute directly. IE: actual machine code.

Whereas language like Java and C# are [often, but not always] compiled into "bytecode" which must be interpretted at runtime. IE: their code is not being run directly, but another program is running its own code that looks at and executes your program. Hence why you need Java installed on your computer to run Java programs, but do not need C++ installed on your computer to run a C++ program.


But this is not always the case. Java and C# can both be very fast. And C++ can also be slow. It depends on a lot of things, as Little Bobby Tables said.
@disch: while i agree with you, bytecode and virtual machines dont have to be slower just because they arent already machine code (actually i read somewhere that some processors can execute java byte code directly but im not sure how accurate that is). cant they perform os specific optimizations? i cant remember who, i think it was cubbi, but they gave a really good post about this
Yes.

OP's question is flawed in a way, as it's making an assumption that isn't necessarily true.
I imagine it's because the processor is actually a physical clone of the JVM, or it's recompiled to the platform's machine language.
Last edited on
closed account (z1CpDjzh)
Thanks
Disch wrote:
Destructors that do nothing cost nothing.
Except that it calls the destructor of the parent class(es) and all the member destructors. "nothing" is relative in this sense...

Disch wrote:
C# and Java still have destructors. GC does not replace them.
Yes, and it is called in this GC prallel thread universe. So 'replaced' may be the wrong term, but it's rather 'suicide' to do anything in the destructor.

Disch wrote:
C and C++ free memory as soon as the object is destroyed.
Only when it comes to the stack. heap memory isn't fee'd automatically (if not using smart pointer)
Disch wrote:
Destructors that do nothing cost nothing.
coder777 wrote:
Except that it calls the destructor of the parent class(es) and all the member destructors. "nothing" is relative in this sense...

Destructors do work that needs to be done. Whether the work is done in destructors or another way in C, the cost is the same. "nothing" is not relative in this sense.

Disch wrote:
C and C++ free memory as soon as the object is destroyed.
Coder777 wrote:
Only when it comes to the stack. heap memory isn't fee'd automatically (if not using smart pointer)

Kind of took that out of context, didn't you? In the context of GC -vs- C++, freeing memory happens when the object is destroyed, generally speaking, and not at some later date.
I had a big reply typed up... .but forget it. This is not worth it.



You said "C is faster than C++ because C++ has destructors". That is an arbitrary, overly simplistic, and false statement. Hence my original reply.
Topic archived. No new replies allowed.