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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:37:21+00:00 2026-05-14T15:37:21+00:00

FIRST PROBLEM I have timed how long it takes to compute the following statements

  • 0

FIRST PROBLEM

I have timed how long it takes to compute the following statements (where V[x] is a time-intensive function call):

Alice     = Table[V[i],{i,1,300},{1000}];
Bob       = Table[Table[V[i],{i,1,300}],{1000}]^tr;
Chris_pre = Table[V[i],{i,1,300}];
Chris     = Table[Chris_pre,{1000}]^tr;

Alice, Bob, and Chris are identical matricies computed 3 slightly different ways. I find that Chris is computed 1000 times faster than Alice and Bob.

It is not surprising that Alice is computed 1000 times slower because, naively, the function V must be called 1000 more times than when Chris is computed. But it is very surprising that Bob is so slow, since he is computed identically to Chris except that Chris stores the intermediate step Chris_pre.

Why does Bob evaluate so slowly?


SECOND PROBLEM

Suppose I want to compile a function in Mathematica of the form

f(x)=x+y

where “y” is a constant fixed at compile time (but which I prefer not to directly replace in the code with its numerical because I want to be able to easily change it). If y’s actual value is y=7.3, and I define

f1=Compile[{x},x+y]
f2=Compile[{x},x+7.3]

then f1 runs 50% slower than f2. How do I make Mathematica replace “y” with “7.3” when f1 is compiled, so that f1 runs as fast as f2?


EDIT:

I found an ugly workaround for the second problem:

f1=ReleaseHold[Hold[Compile[{x},x+z]]/.{z->y}]

There must be a better way…

  • 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-14T15:37:22+00:00Added an answer on May 14, 2026 at 3:37 pm

    You probably should’ve posted these as separate questions, but no worries!

    Problem one

    The problem with Alice is of course what you expect. The problem with Bob is that the inner Table is evaluated once per iteration of the outer Table. This is clearly visible with Trace:

    Trace[Table[Table[i, {i, 1, 3}], {3}]]
    
    {
    Table[Table[i,{i,1,2}],{2}],
    {Table[i,{i,1,2}],{i,1},{i,2},{1,2}},{Table[i,{i,1,2}],{i,1},{i,2},{1,2}},
    {{1,2},{1,2}}
    }
    

    Line breaks added for emphasis, and yeah, the output of Trace on Table is a little weird, but you can see it. Clearly Mathematica could optimize this better, knowing that the outside table has no iterator, but for whatever reason, it doesn’t take that into account. Only Chris does what you want, though you could modify Bob:

    Transpose[Table[Evaluate[Table[V[i],{i,1,300}]],{1000}]]
    

    This looks like it actually outperforms Chris by a factor of two or so, because it doesn’t have to store the intermediate result.

    Problem two

    There’s a simpler solution with Evaluate, though I expect it won’t work with all possible functions to be compiled (i.e. ones that really should be Held):

    f1 = Compile[{x}, Evaluate[x + y]];
    

    You could also use a With:

    With[{y=7.3},
        f1 = Compile[{x}, x + y];
    ]
    

    Or if y is defined elsewhere, use a temporary:

    y = 7.3;
    With[{z = y},
        f1 = Compile[{x}, x + z];
    ]
    

    I’m not an expert on Mathematica’s scoping and evaluation mechanisms, so there could easily be a much better way, but hopefully one of those does it for you!

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

Sidebar

Related Questions

So, i have such problem: first time, then the <a> get clicked e.preventDefault() isn't
I have a problem here im trying to upload a file first time it
i have a problem with triggering the multiple alarm at first time , here
I have a problem with a sheet that when opened for the first time,
I am using $SUB for the first time and have come across this problem.
I have some problem with this mongoid. It's my first time to use mongoDB,
all! I have the following problem: I have 2 simple tables. The first one,
I have got a method that takes a long time to complete and want
I have a function that's taking a long time to run. When I profile
I have a process with a Select which takes a long time to finish,

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.