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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:49:36+00:00 2026-05-16T04:49:36+00:00

I have two ways to program the same functionality. Method 1: doTheWork(int action) {

  • 0

I have two ways to program the same functionality.

Method 1:

doTheWork(int action)
{
    for(int i = 0 i < 1000000000; ++i)
    {
        doAction(action);
    }
}

Method 2:

doTheWork(int action)
{
    switch(action)
    {
    case 1:
        for(int i = 0 i < 1000000000; ++i)
        {
            doAction<1>();
        }
        break;
    case 2:
        for(int i = 0 i < 1000000000; ++i)
        {
            doAction<2>();
        }
        break;
    //-----------------------------------------------
    //... (there are 1000000 cases here)
    //-----------------------------------------------
    case 1000000:
        for(int i = 0 i < 1000000000; ++i)
        {
            doAction<1000000>();
        }
        break;
    }
}

Let’s assume that the function doAction(int action) and the function template<int Action> doAction() consist of about 10 lines of code that will get inlined at compile-time. Calling doAction(#) is equiavalent to doAction<#>() in functionality, but the non-templated doAction(int value) is somewhat slower than template<int Value> doAction(), since some nice optimizations can be done in the code when the argument value is known at compile time.

So my question is, do all the millions of lines of code fill the CPU L1 cache (and more) in the case of the templated function (and thus degrade performance considerably), or does only the lines of doAction<#>() inside the loop currently being run get cached?

  • 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-16T04:49:37+00:00Added an answer on May 16, 2026 at 4:49 am

    It depends on the actual code size – 10 lines of code can be little or much – and of course on the actual machine.

    However, Method 2 violently violates this decades rule of thumb: instructions are cheap, memory access is not.

    Scalability limit

    Your optimizations are usually linear – you might shave off 10, 20 maybe even 30% of execution time. Hitting a cache limit is highly nonlinear – as in “running into a brick wall” nonlinear.

    As soon as your code size significantly exceeds the 2nd/3rd level cache’s size, Method 2 will lose big time, as the following estimation of a high end consumer system shows:

    • DDR3-1333 with 10667MB/s peak memory bandwidth,
    • Intel Core i7 Extreme with ~75000 MIPS

    gives you 10667MB / 75000M = 0.14 bytes per instruction for break even – anything larger, and main memory can’t keep up with the CPU.

    Typical x86 instruction sizes are 2..3 bytes executing in 1..2 cycles (now, granted, this isn’t necessarily the same instructions, as x86 instructions are split up. Still…)
    Typical x64 instruction lengths are even larger.

    How much does your cache help?
    I found the following number (different source, so it’s hard to compare):
    i7 Nehalem L2 cache (256K, >200GB/s bandwidth) which could almost keep up with x86 instructions, but probably not with x64.

    In addition, your L2 cache will kick in completely only if

    • you have perfect prediciton of the next instructions or you don’t have first-run penalty and it fits the cache completely
    • there’s no significant amount of data being processed
    • there’s no significant other code in your “inner loop”
    • there’s no thread executing on this core

    Given that, you can lose much earlier, especially on a CPU/board with smaller caches.

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

Sidebar

Related Questions

The case is this, I have two different forms from the same solution/project. What
I have a program that I can run in two ways: single-end or paired-end
I have observed that there are two ways of executing a perl program: perl
I have two ways of checking if a List is empty or not if
I am trying to introduce RoR to my company and I have two ways
I want to send the form data to server-side. I have two ways: Normal
I have found two ways to extract matches in Python: 1. def extract_matches(regexp, text):
I have a two different ways of representing data in my app: via UITableView
I have an ordered list which I have styled in two ways: Coloured the
I have the following as two ways to declare a two dimensional String array.

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.