Talk:Input number of seconds, get a string back that shows days, hours, minutes, seconds

From Second Life Wiki
Jump to: navigation, search

Redundant use of llFloor()

The use of llFloor() is bizarre in this script, e.g. days=llFloor(secs/86400);

As secs was declared as an integer, secs/86400 is executed as an integer division, which inherently drops any fractional part of the result.

By then using that integer result as an argument of llFloor(), the compiler has to convert the integer to a float (which will simply add .0)

and the llFloor() function has nothing to do (no fraction to remove) so it just converts back to an integer to provide the result.

In other words, your use of llFloor() is redundant throughout this example. Just do the integer division.

Omei Qunhua 01:36, 24 December 2012 (PST)

Further Optimisation

The next stage in abbreviating / simplifying the script, might be to look at repeated similar lines of code, such as adding the ", " and "s". This area is a good candidate for a "user defined function". So we might write a function that could be used 4 times (for days, hours, minutes, and seconds, with appropriate arguments):-

<lsl> string AppendToTimeString(string ResultSoFar, integer n, string text) {

   if (!n)

return (ResultSoFar); // Nothing to add this time, return no changes

   if (ResultSoFar)

ResultSoFar += ", "; // Append the comma separator if the result so far is not empty

   ResultSoFar += ( (string) n + " " + text);      // Append the quantity and corresponding text
   if (n != 1) 	
       ResultSoFar += "s";    		// Append an 's' if not a single quantity
   return (ResultSoFar;

} </lsl>

We could use this user function when processing hours, like this:-

<lsl>

   timeStr = AppendToTimeString (timeStr, secs/3600, "Hour");

</lsl> There we pass the timeStr which has been constructed so far (i.e. from the 'days' stage), the number of hours, and the 'hour' text The user function returns an updated 'timeStr

Hence our final script could look like this:-

<lsl>

string AppendToTimeString(string ResultSoFar, integer n, string text) {

   if (!n)

return (ResultSoFar); // Nothing to add this time, return unchanged string

   if (ResultSoFar)

ResultSoFar += ", "; // Append the comma separator if the result so far is not empty

   ResultSoFar += ( (string) n + " " + text);      // Append the quantity and corresponding text
   if (n != 1) 	
       ResultSoFar += "s";    		// Append an 's' if not a single quantity
   return ResultSoFar;

}

string getTime(integer secs) {

   string timeStr = AppendToTimeString ("", secs/86400, "Day");
   secs = secs % 86400;
   timeStr = AppendToTimeString (timeStr, secs/3600, "Hour");
   secs = secs % 3600;
   timeStr = AppendToTimeString (timeStr, secs/60, "Minute");
   secs = secs % 60;
   return  AppendToTimeString (timeStr, secs, "Second");

}

</lsl>

Omei Qunhua 04:33, 24 December 2012 (PST)

Postscript

The revised code is about half the number of lines of the original. Sadly, tests so far suggest the smaller source code runs about 10% slower, and under Mono is about 976 bytecode, compared to about 848 bytecode for the original! However, I would still tend to write the shorter source code. As a professional programmer I had an oft-quoted adage "every extra line of code you write, is another one you need to test".

Omei Qunhua 06:55, 24 December 2012 (PST)