Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #15306 > unrolled thread

Quantity[] (Units) slows things! Solutions?

Started byFrank Iannarilli <frankeye@cox.net>
First post2013-08-16 07:07 +0000
Last post2013-08-17 06:04 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Quantity[] (Units) slows things!  Solutions? Frank Iannarilli <frankeye@cox.net> - 2013-08-16 07:07 +0000
    Re: Quantity[] (Units) slows things!  Solutions? "Kevin J. McCann" <kjm@KevinMcCann.com> - 2013-08-17 06:04 +0000

#15306 — Quantity[] (Units) slows things! Solutions?

FromFrank Iannarilli <frankeye@cox.net>
Date2013-08-16 07:07 +0000
SubjectQuantity[] (Units) slows things! Solutions?
Message-ID<kukj38$qt6$1@smc.vnet.net>
Hi,

This is for Mathematica9 which introduced units-handling via the Quantity[] function and its friends. It's a nice provision, but can slow things down substantially for production work, e.g. below shows a factor of 6500 slowdown!

For example:
  
lwc[dBZ_, sigma_, n_Quantity] := (1/6) Exp[-9 sigma^2/2] Pi 
   Quantity[1.,"Grams"/("Centimeters"^3)] 
   Sqrt[n (10^(dBZ/10)) 
   Quantity[1., ("Millimeters")^6/("Meters"^3)]]

Timing[Table[lwc[dbz, 0.35, 
   Quantity[200., 1/("Centimeters"^3)]], 
   {dbz, -100., -10., 0.1}]]

===>>65 Seconds!

whereas the unitless equivalent:

lwcn[dBZ_, sigma_, n_] := (1/6) Exp[-9 sigma^2/2] Pi 
    1.0 Sqrt[n (10^(dBZ/10)) ]

Timing[Table[lwcn[dbz, 0.35, 200.], {dbz, -100., -10., 0.1}]]

===>>0.01 Seconds!


Can anyone suggest any tricks other than removing the units-handling?



[toc] | [next] | [standalone]


#15325

From"Kevin J. McCann" <kjm@KevinMcCann.com>
Date2013-08-17 06:04 +0000
Message-ID<kun3oo$3o7$1@smc.vnet.net>
In reply to#15306
I find the new way Mathematica deals with units and physical constants to be a 
rather poor change to the old Units and PhysicalConstants packages. In 
addition to the fact that asking for something like the speed of light 
requires an internet connection, there is the slowdown in just the 
handling of units as you found.

I used the old units package, which has its own problems, to rewrite:

<< Units`

lwc2[dBZ_, sigma_, n_] :=
    Convert[((1/6)*Pi*(Gram/Centimeter^3)*
           Sqrt[n*10^(dBZ/10)*
               ((Milli*Meter)^6/Meter^3)])/
        E^((9*sigma^2)/2),
      Gram/Centimeter^3]

Then

Timing[Table[
   lwc2[dbz, 0.35, 200./(Centimeter^3)], {dbz, -100., -10., 0.1}]]

Takes about 0.5s as opposed to 23.5s on my computer.

Kevin

On 8/16/2013 3:07 AM, Frank Iannarilli wrote:
> Hi,
>
> This is for Mathematica9 which introduced units-handling via the Quantity[] function and its friends. It's a nice provision, but can slow things down substantially for production work, e.g. below shows a factor of 6500 slowdown!
>
> For example:
>
> lwc[dBZ_, sigma_, n_Quantity] := (1/6) Exp[-9 sigma^2/2] Pi
>     Quantity[1.,"Grams"/("Centimeters"^3)]
>     Sqrt[n (10^(dBZ/10))
>     Quantity[1., ("Millimeters")^6/("Meters"^3)]]
>
> Timing[Table[lwc[dbz, 0.35,
>     Quantity[200., 1/("Centimeters"^3)]],
>     {dbz, -100., -10., 0.1}]]
>
> ===>>65 Seconds!
>
> whereas the unitless equivalent:
>
> lwcn[dBZ_, sigma_, n_] := (1/6) Exp[-9 sigma^2/2] Pi
>      1.0 Sqrt[n (10^(dBZ/10)) ]
>
> Timing[Table[lwcn[dbz, 0.35, 200.], {dbz, -100., -10., 0.1}]]
>
> ===>>0.01 Seconds!
>
>
> Can anyone suggest any tricks other than removing the units-handling?
>
>
>
>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web