First, to avoid confusion, take a look at an example whose results all appear as precision numbers of hardware machines that should be less
In[1]:= $MaxMachineNumber Out[1]= 1.79769*10^308
In your general example, this was already a nice (and quick) property. Here's an example from your Times example using machine numbers:
In[2]:= lst = RandomReal[{0.99, 1.01}, 5000000]; Times @@ lst // Timing Out[3]= {1.435, 1.38851*10^-38}
Now we can use Compile to make a compiled function for this operation to work effectively:
In[4]:= listproduct = Compile[{{l, _Real, 1}}, Module[{tot = 1.}, Do[tot *= x, {x, l}]; tot]] Out[4]= CompiledFunction[{l},Module[{tot=1.},Do[tot*=x,{x,l}];tot],-CompiledCode-]
This is much faster:
In[5]:= listproduct[lst] // Timing Out[5]= {0.141, 1.38851*10^-38}
Assuming you have a C compiler and Mathematica 8, you can also automatically compile the entire path to the C code. A temporary DLL is created and bound to Mathematica at runtime.
In[6]:= compiledlistproduct = Compile[{{l, _Real, 1}}, Module[{tot = 1.}, Do[tot *= x, {x, l}]; tot], CompilationTarget -> "C"] Out[6]= CompiledFunction[{l},Module[{tot=1.},Do[tot*=x,{x,l}];tot],-CompiledCode-]
This gives a performance that is not much different from what the Mathematica built-in function would have:
In[7]:= compiledlistproduct[lst] // Timing Out[7]= {0.015, 1.38851*10^-38}
Please note that if your product really goes beyond $ MaxMachineNumber (or $ MinMachineNumber ), then you better stick to Apply[Times, list] . The same comment applies to Total if your results can be so large:
In[11]:= lst = RandomReal[10^305, 5000000]; Plus @@ lst // Timing Out[12]= {1.435, 2.499873364498981*10^311} In[13]:= lst = RandomReal[10^305, 5000000]; Total[lst] // Timing Out[14]= {1.576, 2.500061580905602*10^311}