Click here to Skip to main content
14,177,621 members
Rate this:
Please Sign up or sign in to vote.
See more:

I'm experienced c# dev, but I got a task to port some algorithms into cpp due to performance. While my whole life days were nice and shiny when c# just-in-time compiler and optimizations worked hard 5 to 9 to make my sandbox clean, today I faced cpp performance questions where I feel having lack of low-level/architecture/compiler knowledge.

The problem:
I have some different level methods for example: A, B, C, D. Where A is calling B many times. B is calling C many times, C is calling D many times and so on. So when I started writing macros for deepest level methods everything was nice and performance improved. Lest say I rewrote D macro, then rewrote C macro (which calls D macro in it self), then rewrote B macro. And performance was increasing each time I wrote macro. But now I wrote A macro and performance dropped so rapidly it even got worse than doing everything with no macros at all.

What's happening? Is it some sort of method choke where it goes out of stack and cpu starts his non-algorithm related stuff to allocate/copy memory to proceed this huge macro? What are the tips or rule of thumbs when writing macros or how to solve this problem or how to port math related algorithms to cpp most sufficiently?

Why not inline?[^] says I can't use them if "The function has a variable argument list."
Sergey Alexandrovich Kryukov 13-May-15 16:29pm
Macros? In C++?! What do you mean? Or methods (functions)? Anyway, you did not provide enough information to judge on performance, which depends on very many factors.
Tips? Suggestions? Keep writing in C# but write better quality code. :-)
Philippe Mori 14-May-15 12:57pm
Never use macros if you don't have to. Uses inline functions if you really need. In practice, more often that not the performance is related more to the algorithm that function call overhead. Without code, it is not possib'e to give advice... Except that I highly recommand you to avoid macros.
By the way, are you sure you need C++ code.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Don't use macros for this. Use __inline for inline expansion of the called function.

For variable arguments, you may need to define multiple overloads - to match your varying needs.

We could offer more specific advice if you post your code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190526.1 | Last Updated 13 May 2015
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100