New answers tagged

0

It is handy to use DateString for formatting the time: SetAttributes[myAbsoluteTiming, HoldAll]; myAbsoluteTiming[calculation_] := Module[{startTime, hms, result}, startTime = SessionTime[]; result = calculation; hms = DateString[SessionTime[] - startTime, {"Hour", ":", "Minute", ":", "Second"}]; {hms, result}] It is quite efficient: ...


1

If the digit and month characters always appear in the exact same place you can use StringTake to extract them and a lookup table for all the conversions: a = Association@Join[ StringPadLeft[ToString[#], 2, "0"] -> # & /@ Range[0, 99], {"Jan" -> 1, "Feb" -> 2, "Mar" -> 3, "Apr" -> 4, "May" -> 5, "Jun" -> 6, "Jul" -> ...


6

If the date format is sufficiently rigid, you might try string patterns or regular expressions. AbsoluteTime[{"05-Mar-2004 10:15:00", {"Day", "-", "MonthNameShort", "-", "Year", " ", "Hour24", ":", "Minute", ":", "Second"}}] // RepeatedTiming (* {0.00043, 3287470500} *) This is about 10 times faster: months = <|"Jan" -> 1, "Feb" -> 2, ...


1

Here's my take, borrowing some code from here and here: ic = Function[x, With[{r = Round[x]}, r + Chop[x - r]], Listable]; SetAttributes[myTiming, HoldAllComplete]; myTiming[calculation_, tf : (Timing | AbsoluteTiming) : AbsoluteTiming] := Module[{timing = tf[calculation]}, Print[StringTemplate["`h` hr `m` min `s` s", ...


1

I prefer @Karsten's approach of making a pure function that can be applied directly to format results from AbsoluteTiming, but if you're new to Mathematica this might be easier to follow: SetAttributes[myAbsoluteTiming, HoldAllComplete]; myAbsoluteTiming[calculation_] := Module[{time, result, hours, minutes, seconds, format}, {time, result} = ...


4

SetAttributes[hmsAbsTiming, HoldAllComplete]; hmsAbsTiming[calculation_] := MapAt[IntegerDigits[IntegerPart[#], MixedRadix[{24, 60, 60}]] &, AbsoluteTiming[ calculation ], 1] If you prefer a Quantity object: SetAttributes[hmsAbsTiming2, HoldAllComplete]; hmsAbsTiming2[calculation_] := MapAt[UnitConvert[Quantity[#, "Seconds"], ...



Top 50 recent answers are included