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

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 ask a question.

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

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9247147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:39:34+00:00 2026-06-18T09:39:34+00:00

I’m trying to implement the first calculator. My old code (switch-case) : enum arithmetic_type{

  • 0

I’m trying to implement the first calculator. My old code (switch-case) :

enum arithmetic_type{
    add = 0,
    subtract = 1,
    multiply = 2,
    divide = 3
};

inline void calculate(double &var, double value, arithmetic_type type){

    switch(type)
    {
        case add : var += value;break;
        case subtract : var -= value;break;
        case multiply : var *= value;break;
        case divide : var /= value;break;
    }

}

I saw “pointer to function definition” and then had a new idea : Using separate functions instead. Now my code looks like :

typedef void(*arithmetic_type)(double &var, double value); //template

inline void add(double &var, double value){var+=value;} //components
inline void subtract(double &var, double value){var-=value;}
inline void multiply(double &var, double value){var*=value;}
inline void divide(double &var, double value){var/=value;}
////////////////////////////////////////////////////////////////
struct VAR
{
    double var_value;
    arithmetic_type operator_type;
    inline void calculate(double value){operator_type(var_value, value);}
};

I see it’s much simpler than switch-case. And more importantly, I’m going to add some other operators e.g relational operators… So I think this new solution is clearer and also it’s more convenient than the old switch-case solution. 🙂

But I still doubt about code speed & performance. Does it perform faster?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:39:36+00:00Added an answer on June 18, 2026 at 9:39 am

    First of all – as a general rule you have finite resources. In many cases your most valuable resource is time of programming. In such case you should go with easiest/most readable version. From description of your problem it looks like you have started optimizing prematurely (unless it is toy example in optimization).

    That said in some cases you care about performance. Usually then you have existing implementation but the performance does not meet the goal (say – computation takes days instead of hours). I doubt that ‘simple calculator’ came near close to such problems but lets assume it for the sake of argument. Then there is whole branch of macro-optimalization you can use – i.e. you should think more about big-picture (change of algorithm etc.) rather then small picture (function pointer vs case). First of all you need to find out what is causing slowdown (if something takes 10% of time program is running and you speed it up by 50% the overall improvement will be 5%, on the other hand if you improve the rest of program by 25% the performance will be improved by 22%).

    In even more rare cases even after such optimization the performance goal is high enough that your code still don’t match it. IT IS USUALLY ONLY WHEN YOUR PROGRAM IS COMPUTE HEAVY AND RUNS FOR MONTHS OR YEARS ON MANY MACHINES – for example protein folding is worth optimizing in such way while most of the programs, even popular ones, is not. Usually at this point you need to know:

    • The exact platform you are optimizing for
    • Basic computer architecture (pipelining, ILP, cache architecture, cache-coherence protocol etc.)
    • The compiler you are using

    If you are in this are it is likely that case/switch performs better because it allows better utilize the branch predictions while using function pointer you are only using BTB. On the other hand if your architecture and you are using very simple C compiler the case/switch might overflow the I-cache (so function pointers will perform better).


    To sum up:

    1. Don’t optimize
    2. Don’t optimize yet (for experts only)

    While it is true that in some cases you need to optimize usually you are better off starting from macro-optimalization and recognizing what is source of slowdown. If your program is waiting for I/O anyway (case I would suspect with calculator) then noone will ever know if your program replies a ns faster. Similarly if your parser/tokenizer is the bottleneck optimization of execution won’t help much.

    If macro-optimalizations are not sufficient and 1% improvement of running time can be worth weeks of your work you might want to look at micro-optimalizations knowing both the processor and compiler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.