-5

If I make a c++ project in visual studio, but only use c functions and libraries, will I still get the speed of c? If not, how can I get a c project to run fast, should I still use visual studios?

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Matt
  • 2,232
  • 8
  • 35
  • 64
  • 1
    yes, same speed – Sven Nilsson Jan 19 '17 at 15:28
  • 4
    Although C++ looks like C, it is a different language, and may fail to compile C code. – rustyx Jan 19 '17 at 15:29
  • As you are compiling with two different compilers it could be quicker/slower or the same. – Ed Heal Jan 19 '17 at 15:31
  • 2
    Actually, optimised C++ code is often faster than C. – Kamil Koczurek Jan 19 '17 at 15:32
  • 1
    Speed is dependent on many things, I guess one of the large factors here will be the compiler. If you're using msvc, I wish you luck in getting C code to run as fast as the equivalent in Java, never mind C++. – George Jan 19 '17 at 15:32
  • 3
    My usual advice is to [race your horses](https://ericlippert.com/2012/12/17/performance-rant/). – Filburt Jan 19 '17 at 15:33
  • 1
    C is fast not just because "it's C". It all depends on features used. Any (almost?) C++ code could be rewritten as C and it will have more or less same speed. Hint: Don't worry about speed just because it's cool. Readability and stability are usually more important. – HolyBlackCat Jan 19 '17 at 15:35
  • 1
    Compiling as a different language seems like it will vastly improve the speed with which you can shoot yourself in the foot. – jaggedSpire Jan 19 '17 at 15:37
  • 2
    Why would you only code in C using a C++ compiler? The whole point of using C++ is it's abstractions make writing a complicated system easier. It's almost like buying a sports car and never opening it up. You are not using all the tools available to you. – NathanOliver Jan 19 '17 at 15:37
  • There are many resources online discussing the same ballpark topic. [C vs C++ speed](https://rusty.ozlabs.org/?p=330) and [As Fast as Cee](http://wiki.c2.com/?AsFastAsCee) – Vikas Bhargava Jan 19 '17 at 15:41
  • @HolyBlackCat That's been proven to be not the case. C++ is slower than C when trying to accomplish the same thing. It's very interesting. (http://raid6.com.au/~onlyjob/posts/arena/). Oh and for your "it's more about the readability stability" comment: I think that means you should avoid C++ :) – Dellowar Jan 19 '17 at 15:55
  • @SanchkeDellowar Wrong. For example, C++ std::sort is often faster than C qsort, one reason is compile-time binding of the comparison operator. C++ is the only language that has C-like speed and modern abstractions, most importantly, destructors. – Erik Alapää Jan 19 '17 at 16:32
  • @SanchkeDellowar Rougly saying C++ is a superset of C, so it must be possible to write C++ code which is at least as fast as C. Also, I agree that some parts of C++ are weird, but it's (usually) more readable than C if used correctly. – HolyBlackCat Jan 19 '17 at 16:43

2 Answers2

4

C is almost C++ subset, but there are cases, where C behave differently. You can find more about this this post: Where is C not a subset of C++?

Nevertheless, the C code compiled as C++ will be same or almost same as C. You can try looking at assembly code produced with g++ -S source.c and gcc -S source.c and compare these two results. Don't forget, that running gcc on cpp file will in fact use C++ compiler! (So, make one file test.c and second one just symlink named test.cpp)

I've tried it with just simple code:

#include <stdio.h>
int main(int argc, char ** argv) {
    printf("Argc: %d, argv[0]: %s\n", argv[0]);
    return 5;
}

And the compiled code is 100% same. It may (and it will) differ, of course, in more complex examples.

Community
  • 1
  • 1
Mike S.
  • 1,995
  • 13
  • 25
  • Doesn't `g++` call C compiler for files with `.c` extension? – Kamil Koczurek Jan 19 '17 at 15:42
  • @Kamil: ... and vice versa. Yes, you are right. Was adding that right in time you answered. – Mike S. Jan 19 '17 at 15:43
  • Any reasonable productive program has different meaning in both languages. Identical syntax does not imply identical semantics. Considering C as "almost a subset of C++" is worse than a bad statement. – too honest for this site Jan 19 '17 at 16:04
  • Yes. And thas why I've posted a link to more detailed page and described a way to try it yourself (with almost no-op example that works the same). Saying just "no" (which would be, after all, too short to be used as answer on Stack Overflow sites), the OP would not learn anything new, just that it don't work the way he thinks. – Mike S. Jan 19 '17 at 16:18
  • Note that with GCC, you can use `-x c` and `-x c++` to force compilation as C or C++, respectively. – Justin Time - Reinstate Monica Jan 26 '17 at 20:49
0

Will C still be fast if I compile it as C++?

It could be faster or slower, but probably the same. Assuming it compiles at all. And assuming the language change didn't change the meaning of the program.

Much bigger problem than speed, is the possibility that the behaviour will change. Some things that have defined behaviour in C, have undefined behaviour in C++.

how can I get a c project to run fast

Keep using a C compiler, and the project will stays as fast as it was, and won't accidentally implode at runtime.

If you want / need to use a C++ compiler, then translate your project to C++.

eerorika
  • 232,697
  • 12
  • 197
  • 326