J
jehugaleahsa
Hello:
I have a List<T[]> where the internal arrays are of a fixed size.
I want to be able to take an index of, say, {1, 3} and quickly move to
the next nth position. So say I have a method that looks like this:
public struct Position
{
Position(int x, int y) { X = x; Y = y; }
int X { get; set; }
int Y { get; set; }
}
private Position Advance(Position pos, int distance)
{
// creates new position that is distance positions forward in the
array
}
I am currently implementing this method like this:
private Position Advance(Position pos, int distance)
{
int offset = pos.X * FIXED_ARRAY_SIZE + pos.Y + distance;
return new Position(offset / FIXED_ARRAY_SIZE, offset %
FIXED_ARRAY_SIZE);
}
However, my profiler is showing that the majority of the time in my
application is being spent on this method, due to the multiplication
and division. I tried using Math.DivRem, but that didn't help very
much at all.
I am wondering if there is a more efficient manner of getting the
offset. Any help would be greatly appreciated.
Thanks,
Travis
I have a List<T[]> where the internal arrays are of a fixed size.
I want to be able to take an index of, say, {1, 3} and quickly move to
the next nth position. So say I have a method that looks like this:
public struct Position
{
Position(int x, int y) { X = x; Y = y; }
int X { get; set; }
int Y { get; set; }
}
private Position Advance(Position pos, int distance)
{
// creates new position that is distance positions forward in the
array
}
I am currently implementing this method like this:
private Position Advance(Position pos, int distance)
{
int offset = pos.X * FIXED_ARRAY_SIZE + pos.Y + distance;
return new Position(offset / FIXED_ARRAY_SIZE, offset %
FIXED_ARRAY_SIZE);
}
However, my profiler is showing that the majority of the time in my
application is being spent on this method, due to the multiplication
and division. I tried using Math.DivRem, but that didn't help very
much at all.
I am wondering if there is a more efficient manner of getting the
offset. Any help would be greatly appreciated.
Thanks,
Travis