B
beginner
Hello,
We are writing a performance critical application. As a result, we use
Array instead of List or any other more sophisticated container types
to store temporary data. However, the array type is of fixed length,
and therefore there is a chance for the index to be out of range. I
expect this to happen once every a hundred times of execution and when
this happens, I'd like to create a bigger array. To prevent the bounds
from being checked every time, I am thinking of the following code (in
sketch):
int [] l= new int[100]; .//temp variable
.....
try
{
int count=0;
while(...)
{
...
l[count++]=f(...); //
....
l[count*2+1]=f(...); //multiple places that put stuff into the
array
}
}
catch(OutOfRange e)
{
//create a bigger array
l2=new int[count*2+100];
//copy partial results from old array to new array
...
//replace old array
l=l2
resume execution right after the exception was thrown
}
But I don't think there is a way in C# to support this pattern.
I think this is a really useful pattern, as it is way more efficient
to deal with such conditions that happen once in a million times.
Thanks,
Geoffrey
We are writing a performance critical application. As a result, we use
Array instead of List or any other more sophisticated container types
to store temporary data. However, the array type is of fixed length,
and therefore there is a chance for the index to be out of range. I
expect this to happen once every a hundred times of execution and when
this happens, I'd like to create a bigger array. To prevent the bounds
from being checked every time, I am thinking of the following code (in
sketch):
int [] l= new int[100]; .//temp variable
.....
try
{
int count=0;
while(...)
{
...
l[count++]=f(...); //
....
l[count*2+1]=f(...); //multiple places that put stuff into the
array
}
}
catch(OutOfRange e)
{
//create a bigger array
l2=new int[count*2+100];
//copy partial results from old array to new array
...
//replace old array
l=l2
resume execution right after the exception was thrown
}
But I don't think there is a way in C# to support this pattern.
I think this is a really useful pattern, as it is way more efficient
to deal with such conditions that happen once in a million times.
Thanks,
Geoffrey