Just some analysis to try to find where the slow down. On my PC, it took 25 seconds to build the table. ps. I never used Units before.
Your main loop:
x = UnitConvert@Quantity["PlanckConstant" "SpeedOfLight"/"BoltzmannConstant"]
x1 = UnitConvert@Quantity[2, "PlanckConstant" ("SpeedOfLight")^2]
B[L_, T_] := (L^(-5))/(Exp[x/(L T)] - 1)
c = Quantity[1000, "Kelvins"]
Table[{Quantity[ll, "Micrometers"],
UnitConvert[x1 B[Quantity[ll, "Micrometers"], c],"Watts"/("Meters")^3]},
{ll, 0.1, 15, 0.2}
]; // Timing
(* {25.693365, Null} *)
CPU Analysis
Raising to -5
powers is fast
Table[Quantity[ll, "Micrometers"]^(-5) , {ll, 0.1, 15,0.2}]; // Timing
(* {0.764405, Null} *)
The Exp
use is also fast
Table[Exp[Quantity[ll, "Micrometers"]] , {ll, 0.1, 15, 0.2}]; // Timing
(* {0.015600, Null} *)
Products of Quantities is little slow, but it depends on which Quantities:
c = Quantity[1000, "Kelvins"]
Table[Quantity[ll, "Micrometers"] c , {ll, 0.1, 15,0.2}]; // Timing
(* {2.886019, Null} (1) *)
Lets go back and add the division now, to try to build the B
function as it was. Dividing quantities now:
x = UnitConvert@Quantity["PlanckConstant" "SpeedOfLight"/"BoltzmannConstant"]
Table[x/Quantity[ll, "Micrometers"], {ll, 0.1, 15, 0.2}]; // Timing
(* {7.347647, Null} (2) *)
Lets add the c
back, now will do products of Quantities, notice CPU doubles *)
Table[x/(Quantity[ll, "Micrometers"] c), {ll, 0.1, 15, 0.2}]; // Timing
(* {14.040090, Null} (3) *)
Add the Exp
back. CPU does not change much as expected
Table[Exp[x/(Quantity[ll, "Micrometers"] c)], {ll, 0.1, 15, 0.2}]; // Timing
(* {14.008890, Null} *)
Add the second division of Quantity you had. Notice no change in CPU since Units are doing division by a number now (no units in result of Exp[...]
, just a number)
Table[ Quantity[ll, "Micrometers"]^(-5)/Exp[x/(Quantity[ll, "Micrometers"] c)],
{ll, 0.1, 15,0.2}]; // Timing
(* {14.882495, Null} *)
Now add x1
back. This is product of Quantity again, Notice the CPU doubles now
x1 = UnitConvert@Quantity[2, "PlanckConstant" ("SpeedOfLight")^2]
Table[ x1 (Quantity[ll,"Micrometers"]^(-5)/Exp[x/(Quantity[ll,"Micrometers"]c)]),
{ll, 0.1, 15,0.2}]; // Timing
(* {24.679358, Null} (4) *)
here we go.
Summary
- Product of "Kelvins" with "Micrometers" about 3 second. step (1)
- division of meter kelvin by "Micrometers" is about 4 seconds. step (2)
- division of "Micrometers" by micro meter kelvin about 7 seconds step (3)
- product of kg meter to the fourth per second cube with micrometer to the fifth 14 seconds. step (4).
Tried to Compile
these, but can't on Units.
Conclusion
product and division between units seems to be slow (compared to just numbers!), it also seems to depend on which units are involved.
More thorough analysis is needed to find why that is and optimize these operations.
p
is rather slow. $\endgroup$