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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:32:46+00:00 2026-05-27T20:32:46+00:00

I was reading a useful post at WRI blog on improving speed of code

  • 0

I was reading a useful post at WRI blog on improving speed of code, and I need help in understanding this one.

Compare these speeds

Timing[
 tbl = Table[i + j, {i, 1, 1000}, {j, 1, 1000}];     
]

{0.031, Null}

and

Timing[
 a = 1000;
 tbl = Table[i + j, {i, 1, a}, {j, 1, a}];
 ]

{0.422, Null}

So it is much faster when putting the actual value for the limit inside the table itself vs outside. The explanation for this, which I am sure it is correct, but I need help in understanding, is that Table is compiled if its limit are numeric vs. not, this is because its Attributes is HoldAll.

But my question is: How would the above actually work, because the limits to Table must, at one point, become numeric anyway? I can’t write

Clear[a]
tbl = Table[i + j, {i, 1, a}, {j, 1, a}]

The above gives an error.

So, for me, writing a=1000 outside Table vs. inside, should have made no difference, since without a having a numerical value, Table[] can’t do anything. So the replacing of a by the number 1000 must occur at one point of time by evaluator before Table[] can do anything useful, would it not?

In other words, what Table should see, eventually, is {i, 1, 1000}, {j, 1, 1000} in both cases.

So, the way I thought this would happen is this:

  1. Evaluator replaces a by 1000 in the arguments of table
  2. Evaluator calls Table with the result, which is now all numeric.
  3. Table Compiles, and runs faster now.

But what seems to happen is something else. (due to HoldAll ?)

  1. Table takes its arguments, as is. Since it has HoldAll, so it sees a and not 1000.
  2. It does not call Compile since its arguments are not all numbers.
  3. It now generate a table with the a limit, Evaluator evaluates a to 1000
  4. Table is generated now all limits are numeric, but slower now since code is not compiled.

Question is: Does the above sort of what happens? Could someone explain the steps that would have happened to explain this difference in timing?

Also, how would one insure that Table is Compiled in both cases in the above example, even if one uses a variable for the limit? It is not always possible to hardcode the numbers for the table limits, but one must sometime use a variables for these. Should one explicitly use the Compile command? (I do not use Compile directly, since I assumed it is done automatically when needed).

edit(1)

In answer to post by Mike below on finding no difference in timing when using a call.

ClearAll[tblFunc];
Timing[a = 1000;
 tblFunc[a_] := Table[i + j, {i, 1, a}, {j, 1, a}];
 Developer`PackedArrayQ[tblFunc[a]]
 ]

gives

{0.031, True}

But that is because a is now the number 1000 INSIDE the function, once it is called. Since M passes things by VALUE.

If we force the call to be by reference, so that a is left unevaluated, then we get

ClearAll[tblFunc];
Timing[a = 1000;
 tblFunc[a_] := Table[i + j, {i, 1, a}, {j, 1, a}];
 Developer`PackedArrayQ[tblFunc[Unevaluated@a]]
 ]

now we see the expected result, since now a is still symbolic INSIDE the function, we are back to square one, and now it is slow, since not packed. And since it is not packed, Compile is not used.

{0.437, False}

edit(2)
Thanks to everyone for the answers, I think I learned allot from them.

Here is an executive summary, just to make sure I got everything ok.

enter image description here

edit(3)

Here are links I have specially related to hints to use to making Mathematica code runs faster.

  1. http://library.wolfram.com/howtos/faster/
  2. http://blog.wolfram.com/2011/12/07/10-tips-for-writing-fast-mathematica-code/
  3. https://stackoverflow.com/questions/4721171/performance-tuning-in-mathematica
  4. Using Array and Table Functions in Mathematica. Which is best when
  • 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-27T20:32:47+00:00Added an answer on May 27, 2026 at 8:32 pm

    So this is what I think is happening. The reason why you see the slow down between a numeric and a symbolic limit on Table is due to the fact that you do a double index. Each sub-table (e.g. going over all indices j for a fixed index i) is constructed separately and when the limit is symbolic there is an extra step involved in figuring out that limit before constructing each sub table. You can see this by examining, e.g.

    Trace[a = 3;
          tbl = Table[i + j, {i, 1, a}, {j, 1, a}];
         ]
    

    David gives a good example for why you would want to do this check for every sub list. As to why Mathematica cannot figure out when this check is not needed I have no clue. If you only have one index to sum over there is no difference in speed between the symbolic and numeric version

    Timing[tbl = Table[i + j, {j, 1, 1000}];]
    {0.0012, Null}
    
    Timing[a = 1000;
           tbl = Table[i + j, {j, 1, a}];
          ]
    {0.0013, Null}
    

    To answer your follow up regarding speed; making tbl a function is faster for both numeric and symbolic limits.

    Timing[a = 1000;
           tblFunc[a_] := Table[i + j, {i, 1, a}, {j, 1, a}];
           tblFunc[a];
          ]
    
    {0.045171, Null}
    

    vs.

    Timing[tbl = Table[i + j, {i, 1, 1000}, {j, 1, 1000}];]
    {0.066864, Null}
    
    Timing[a = 1000;
           tbl = Table[i + j, {i, 1, a}, {j, 1, a}];
          ]
    {0.632128, Null}
    

    You gain even more speed if you intend to reuse the tbl construction.

    b=1000;
    Timing[tblFunc[b];]
    {0.000013, Null}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was reading Reto Meier's (very useful) Android developer blog post A Deep Dive
I was reading this blog post: http://www.shakie.co.uk/ramblings/feature-driven-development-riding-the-waves-of-change/ and came across the part about TestDox:
I was reading this article on GPU speed vs CPU speed. Since a CPU
I'm just getting started with REST and I've been reading this post and the
I was reading this very useful article and noticed that a Windows Phone application
Before reading the question: This question is not about how useful it is to
For people out there using Haxe , what makes it useful for you? Reading
Reading Java Concurrency In Practice, there's this part in section 3.5: public Holder holder;
I was reading this tutorial from the DNN website http://www.dotnetnuke.com/Community/Blogs/tabid/825/EntryId/2675/DotNetNuke-Skinning-101-Part-2.aspx I found the tutorial
Reading through the C specs I found this function: double remquo(double x, double y,

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.