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

  • Home
  • SEARCH
  • 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 8217297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:24:47+00:00 2026-06-07T12:24:47+00:00

Which is better optimization? A series of if/else statement which receives the ‘string’ returns

  • 0

Which is better optimization?

  • A series of if/else statement which receives the ‘string’ returns the appropriate function for it. (Around 40-50 if/else statements).
  • A dictionary maintaining the key-value pair. key as strings, and values as the function objects, and one main function to search and return the function object.

The main function which actually returns the function object using above method would be called millions or billions of times, so need to do this intelligently. What could be the better way?

For e.g.

dict['str1'] = func1
dict['str2'] = func2
and so on..

def main_func(str):
    return dict[str]

Or

def main_func(str):
    if 'str1':
      return func1
    elif 'str2':
      return func2

Which would be better..? if we have 50-60 such strings, and this process needs to be billions of times.

Storing function object inside dictionary, in function itself:-

def func1():
   if dict.has_key('str1'):
        dict['str1'] = func1
   -- do something --

Which is better, this or the above one. This looks much cleaner.? But remember, these functions would be called many times so has_key function would also be called many times.

Thanks

  • 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-07T12:24:48+00:00Added an answer on June 7, 2026 at 12:24 pm

    Choose the dictionary.

    The dictionary …

    • is built-in
    • is pythonic
    • requires less boilerplate code
    • has O(1) complexity, compared to the if-else linear O(n) complexity
    • isn’t guilty of premature pessimization (we have insufficient reason to believe without profiling that it is a less efficient method by a large margin)

    I would suggest writing the solution using a dictionary first and seeing if the solution is fast enough for your needs. If so, great, you’re done. If not, time it against the other way.

    Consider a solution like this (which will return None if the string is not found):

    func_dict = {}
    func_dict['str1'] = fun1
    func_dict['str2'] = fun2
    ...
    def function_lookup(func_string):
        return func_dict.get(func_string)
    

    Then, in your main, simply write function_lookup(whatever_string_variable) to attempt a lookup for your function. This avoids the dictionary from being rebuilt every time function_lookup is called.

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

Sidebar

Related Questions

Which is better in terms of CPU optimization for a web server? Writing plain
which is better for url optimization: dictionary.com/w/apple vs dictionary.com/words/apple ?
Which is better? 1. = link_to Page, /page, :class => button, :data => {:theme
which is better or faster, A or B? std::deque<Myclass> queue; ... // do something
Which constitutes better object oriented design? Class User { id {get;set} } Class Office
Which is better? if not var: var = get_var() (or) var = var or
Which is better way to use jQuery() ? I've heard that jQuery(element) doesn't ruin
Which is better in respect to performance and memory utilization? // + Operator oMessage.Subject
Which has better execution performance: CSS3 animations, or jQuery animations? How do they affect
Is there any considerations to determine which is better practice for creating an object

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.