What is the most efficient way to truncate a number for a specific accuracy?
views:
85answers:
3
+1
A:
int ms = Convert.ToInt32(
Convert.ToString(DateTime.Now.Millisecond).Substring(0, 3));
or
double Length = Math.Pow(10, (DateTime.Now.Millisecond.ToString().Length - 3));
double Truncate = Math.Truncate((double)DateTime.Now.Millisecond / Length);
EDIT:
After running both the below on the code I will post, the double method works well due to reuse of variables. Over an iteration of 5,000,000 DateTime.Now's (in which many will be skipped by both checks), the SubString()
method took 9598ms, and the Double
method took 6754ms.
EDIT#2: Edited in * 1000 into tests to make sure the iterations are running.
Code used to test as follows:
Stopwatch stop = new Stopwatch();
stop.Start();
for (int i = 0; i < 5000000; i++)
{
int MSNow = DateTime.Now.Millisecond * 1000;
if (MSNow.ToString().Length > 2)
{
int ms = Convert.ToInt32(
Convert.ToString(MSNow).Substring(0, 3));
}
}
stop.Stop();
Console.WriteLine(stop.ElapsedMilliseconds);
stop = new Stopwatch();
stop.Start();
for (int i = 0; i < 5000000; i++)
{
int MSNow = DateTime.Now.Millisecond * 1000;
int lengthMS = MSNow.ToString().Length;
if (lengthMS > 2)
{
double Length = Math.Pow(10, (lengthMS - 3));
double Truncate = Math.Truncate((double)MSNow / Length);
}
}
stop.Stop();
Console.Write(stop.ElapsedMilliseconds);
Console.ReadKey();
Kyle Rozendo
2010-05-20 09:03:52
What is the most *efficient* way?
stacker
2010-05-20 09:14:40
@stacker - Take both, and run them through a `Stopwatch` and see which is faster?
Kyle Rozendo
2010-05-20 09:16:49
I would say the `Math.Pow` one is much slower, but it's just a guess, test it out with a `StopWatch`.
IVlad
2010-05-20 09:18:10
@IVlad, Im guessing so too, with the `double` calculations. Im doing the calculation now to see.
Kyle Rozendo
2010-05-20 09:20:04
@IVlad - Interestingly not.
Kyle Rozendo
2010-05-20 09:37:33
@Stacker - There's some data for you.
Kyle Rozendo
2010-05-20 09:38:40
`string` is always slower.
stacker
2010-05-20 09:50:46
@stacker - not necessarily. In the beginning tests without reuse of parameters, the string method was substantially faster. It all comes down to how you code it in the end.
Kyle Rozendo
2010-05-20 09:53:35
this is really simple math problem. I think there is more simple solution.
stacker
2010-05-20 10:36:03
@stacker - The solution is pretty simple. You take 10 to the power of the Length of the number, minus the precision, Divide the number by the power and truncate it: `Trunc(Number / (10^Number.Length - Precision))`
Kyle Rozendo
2010-05-20 11:18:50
+3
A:
In a DateTime
, Milliseconds are always comprised between 0 and 999 so you don't have anything to do.
Julien Lebosquain
2010-05-20 09:10:34
Quoting the MSDN link I posted in my answer "The milliseconds component, expressed as a value between 0 and 999." Seems logical you can't have 32 days in a month nor more than 999ms in a second.
Julien Lebosquain
2010-05-20 09:25:00
A:
Math.Floor(num * Math.Pow(10, x) + 0.5) / Math.Pow(10, x)
Where x your precision
Mubashar Ahmad
2010-05-20 10:48:53
Well I think i have given answer out of context. however i thought you are asking for Rounding, Anyway i can't get what does truncate even means in numbers, Anyway sorry for not picking up the context.
Mubashar Ahmad
2010-05-20 10:53:10