When you run the code in the command window, Matlab's JIT acceleration cannot or does not optimize the execution time, or perhaps only partially. The JIT is subject to changes between versions, so I stopped to check this. The JIT identifies variables processed in loops and if they do not change their type, they are addressed faster - at least it looks like, because the JIT is not fully documented. When you have many variables in the workspace and Matlab has to access one, it must search in the lookup table for symbols, to find out, if a symbol is a variable, function, java class, etc. This lookup is time-consuming and a huge workspace can slow this down.
An evil example code:
r = 1;
a = 0;
for i = 1:1000
a = a + r * rand;
if rand < 0.0001
r = @myFunctionWhichReplies_31415
Now Matlab has to lookup the symbol "r" in each iteration. Even if this code is included in a function, the JIT cannot avoid this. But without this evil change, the loop is accelerated by recognizing that "r" is always the same array.
As soon as an eval or assignin('caller') modifies the lookup table for symbols, the JIT cannot optimize the code anymore and the speed of loops can degrade substantially. Changing the type of a variable anywhere in the code can impede the acceleration also.
So, this answer does not match the question, but it explains, why running code in the command window with a large base workspace can slowdown the execution such that the observed differences can happen.
[EDITED] As Philip Borghesani has mentioned in the comments: The JIT is developping and not documented. Some of the statements of this answer might concern certain Matlab releases only. It is questionable, if code should be optimized for a specific JIT version, but it is a good practice to try at least not to impede this tool.