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 599801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:32:15+00:00 2026-05-13T16:32:15+00:00

Is it better if I do this: foreach my $item ( @array ) {

  • 0

Is it better if I do this:

foreach my $item ( @array ) {
   if ( $bool ) {
     .. code ..
   }
   else {
     .. code ..
   }
}

or

if ( $bool ) {
   foreach my $item ( @array ) {
   }
}
else {
   foreach my $item ( @array ) {
   }
}
  • 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-05-13T16:32:15+00:00Added an answer on May 13, 2026 at 4:32 pm

    I would leave premature optimization aside.

    “Premature optimization is the root of all evil” – Donald Knuth

    You should go for maintainability first and foremost. Group them in the way that makes more sense taking into account the logical structure of the code (such as grouping related statements together).

    If you later determine that performance is an issue, try measuring with something like a profiler to see where the bottlenecks are. Chances are, it’s not there. From Code Complete 2:

    Barry Boehm reports that 20 percent of
    a program’s routines consume 80
    percent of its execution time. In his
    classic paper “An Empirical Study of
    Fortran Programs,” Donald Knuth found
    that less than four percent of a
    program usually accounts for more than
    50 percent of its run time.

    We shouldn’t try to guess where to optimize before it is necessary since most of us are really bad at guessing where that slow portion of our code is. Programmers who optimize as they go also spend about 96% of their time optimizing code that doesn’t need to be optimized. Another thing to take into account is that code tuning (as in this example) considers a tradeoff between readability and maintainability for performance:

    Focusing on optimization during
    initial development detracts from
    achieving other program objectives.
    Developers immerse themselves in
    algorithm analysis and arcane debates
    that in the end don’t contribute much
    value to the user. Concerns such as
    correctness, information hiding, and
    readability become secondary goals,
    even though performance is easier to
    improve later than these other
    concerns are. Post hoc performance
    work typically affects less than five
    percent of a program’s code. Would you
    rather go back and do performance work
    on five percent of the code or
    readability work on 100 percent?

    I’m not saying don’t optimize, but optimize code only in the end, when you have the luxury of the big picture and tools to point you in the right direction.

    EXTRA: To answer the question of performance itself, though:

    This [“unswitching” the code] is good for about a 20 percent time savings:

    Language        Straight Time    Code-Tuned Time    Time Savings
    C++             2.81             2.27               19%     
    Java            3.97             3.12               21%
    Visual Basic    2.78             2.77               <1%
    Python          8.14             5.87               28%
    

    A hazard distinct to this case is that the two loops have to be maintained in parallel.
    […] you have to remember changing the code in both places, which is an annoyance for
    you and a maintenance headache for anyone else who has to work with the code.

    This example also illustrates a key challenge in code tuning: the effect of any specific
    code tuning is not predictable. The code tuning produced significant improvements in
    three of the four languages but not in Visual Basic. To perform this specific
    optimization in this specific version of Visual Basic would produce less maintainable
    code without any offsetting gain in performance. The general lesson is that you must
    measure the effect of each specific optimization to be sure of its effect – no
    exceptions.

    Check this other question here on SO. And this from the first edition of Code Complete.

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

Sidebar

Ask A Question

Stats

  • Questions 405k
  • Answers 405k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer maybe you have to turn on the passive mode by… May 15, 2026 at 5:55 am
  • Editorial Team
    Editorial Team added an answer You need to manually uninstall the service first, see: http://msdn.microsoft.com/en-us/library/sd8zc8ha(VS.80).aspx May 15, 2026 at 5:55 am
  • Editorial Team
    Editorial Team added an answer You can use %hhd under glibc's scanf(), MSVC does not… May 15, 2026 at 5:55 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.