Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Browse

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to add post.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

OrkTouch

OrkTouch Logo OrkTouch Logo

Mobile menu

Close
Ask a Question
  • Home
  • Questions
  • Articles
  • News
  • Members
  • Groups
  • Badges
  • About Us |
  • Contact Us |
  • FAQs |
  • DMCA |
  • Privacy Policy |
  • Terms of Service
Home/ Questions/Q 6333
adam
  • 0
adamPundit
Asked: 15 April 20222022-04-15T11:28:12+05:30 2022-04-15T11:28:12+05:30In: Coding

Why is C faster than C++?

  • 0

Much is misunderstood about the similarities between C and C++. While C++ began as a superset of contemporaneous C, both languages have moved on. Just as we are not simply supersets of one or both of our parents.

There are specific cases where it is possible to create a source file that can be compiled as either C or C++ and, using the same compiler with the same compile flags, produce the same executable object with the same instructions.

Empirically, these have the same performance.

For much of the mutual lifespan of the two languages, C was typically the fastest, supra-assembly, language for many scenarios because it was most capable of a 1:1 statement:instruction coding.

  • a = 1; // maps to arm: `mov w20, 1`

Unfortunately, the majority of today’s architectures are highly complex with multiple tiers of cache, speculative execution, predictors, and deep pipelining.

Assuming you are using a modern compiler on modern architecture – clang, gcc, msvc; x86_64, arm64, amd64, m1 – then the C and C++ compilations will share a huge number of codepaths and features.

Early C++ – that is the features distinct from C such as class methods etc – often suffered runtime performance overheads.

  • // C implementation
  • void execute(int (*op)(int, int), int l, int r, int* out)
  • {
  • if (op) *out = op(l, r);
  • }
  • // C++ implementation
  • struct S
  • {
  • virtual int operator()(int l, int r);
  • };
  • void execute(S* op, int l, int r, int& out)
  • {
  • if (op) out = op->operator(l, r);
  • }

In the C implementation, the way we use ‘op’ may make it quite obvious it will have no more overhead than a regular function call.

The C++ implementation tho is not so clear, and older C++ compilers would indeed have had to do an indirection:

ccoding
  • 0 0 Answers
  • 11 Views
  • 0 Followers
  • 0
  • Share
    Share
    • Share on Facebook
    • Share on Twitter
    • Share on LinkedIn
    • Share on WhatsApp

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
Add A New Post
Submit a URL

Top Members

aseo8889991

aseo8889991

  • 0 Questions
  • 42 Points
Beginner
hpnkntf

hpnkntf

  • 6 Questions
  • 37 Points
Beginner
RuytElmer

RuytElmer

  • 0 Questions
  • 36 Points
Beginner
Nathaniel

Nathaniel

  • 0 Questions
  • 36 Points
Beginner

Trending Tags

analytics c coding company computer dfs english golden goose google javascript language php programming programs russia-ukrain sdf sfd spirit airlines ticket

Explore

  • Home
  • Questions
  • Articles
  • News
  • Members
  • Groups
  • Badges
  • About Us |
  • Contact Us |
  • FAQs |
  • DMCA |
  • Privacy Policy |
  • Terms of Service

© 2020 OrkTouch. All Rights Reserved