# Two strange efficiency problems in Mathematica

Filed under:
|
|
|
|
##### table

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?

Many thanks!

© Stack Overflow or respective owner

### Related posts about mathematica

• #### Debugging a working program on Mathematica 5 with Mathematica 7

as seen on Stack Overflow - Search for 'Stack Overflow'
Hi everybody, I'm currently reading the Mathematica Guidebooks for Programming and I was trying to work out one of the very first program of the book. Basically, when I run the following program: Plot3D[{Re[Exp[1/(x + I y)]]}, {x, -0.02, 0.022}, {y, -0.04, 0.042}, PlotRange -> {-1, 8}, PlotPoints… >>> More

• #### Mathematica 8 crashes Ubuntu 13.10

as seen on Ask Ubuntu - Search for 'Ask Ubuntu'
I have Mathematica 8 installed on my Ubuntu laptop since 2011. I updated Ubuntu several times, and experienced no problems with Mathematica. It also worked smoothly after I updated Ubuntu to 13.10 (it worked for sure for a week after update). When I tried to start Mathematica today by executing… >>> More

• #### What is in your Mathematica tool bag?

as seen on Stack Overflow - Search for 'Stack Overflow'
We all know that Mathematica is great, but it also often lacks critical functionality. What kind of external packages / tools / resources do you use with Mathematica? I'll edit (and invite anyone else to do so too) this main post to include resources which are focused on general applicability in… >>> More

• #### Running Mathematica-5 remotely

as seen on Server Fault - Search for 'Server Fault'
I have Mathematica 5 - a powerful CAS. I have a cheap netbook (running Windows XP), wich not only is too slow to run mathmatica on, I doubt it has the harddrive space. I do however have remote access to a number of very powerful computers, (most of wich run variose Linuxes, but one of which is Windows… >>> More

• #### How to extract a Date from an SQLDateTime object in Mathematica

as seen on Stack Overflow - Search for 'Stack Overflow'
I am trying to do a plot of a time series with DateListPlot. I want to feed it a time series I obtain from an SQL database. When I retrieve the time series the list is composed of SQLDateTime entries that DateListPlot doesn't understand. In[24]:= t=SQLExecute[conn, "select timestamp,value from… >>> More

### Related posts about optimization

• #### Search Engine Optimization - The Importance of Page Optimization in Search Engine Optimization

as seen on Ezine Articles - Search for 'Ezine Articles'
In order for your website to rank well, your internal linking structure is critical to your success. This is covered some of the theory for this in various articles and blogs about Page Structure of a website, which said how you should map out the physical linking structure, but in this guide I will… >>> More

• #### SEO Optimization - How to Master the SEO Optimization Process in Four Easy Steps

as seen on Ezine Articles - Search for 'Ezine Articles'
Search engine optimization is a critical part of any internet marketing campaign but can often be intimidating to new marketers. In this article you will learn the 4 basic components of SEO. >>> More

• #### Keywords Optimization For Website Optimization

as seen on Ezine Articles - Search for 'Ezine Articles'
Saying that you need to do website optimization sounds like saying you need to get healthy. To get healthy we do 2 things: diet management and exercise. Lets start with diet management. Keywords are like food for your WebPages. This article explains the role of keywords in website optimization. >>> More

• #### Importance of On-Page Optimization in Search Engine Optimization (SEO)

as seen on Ezine Articles - Search for 'Ezine Articles'
On-page optimization is crucial for any website. Read the importance of on-page optimization and how it would be helpful for getting high ranking in search engines. >>> More

• #### The Expert Secret to Search Engine Optimization - Effective Website Optimization

as seen on Ezine Articles - Search for 'Ezine Articles'
Throwing keywords into a program that shows you how popular they are and then using those keywords without doing a little bit of preliminary research and answering some very important questions can just spell disaster. There are three questions that are extremely important to ask yourself before just… >>> More