|
|
|
start date: Thu, 23 Aug 2007 02:45:08 -0700,
posted on: microsoft.public.dotnet.languages.csharp
back
| Thread Index |
|
1
Francisco
|
|
2
Rick Lones
|
|
3
Peter Duniho
|
Efficient Array<> of valuetype entry manipulation
Hello,
Is there any code faster than this array position manipulation (some
code omitted for brevity)?:
internal struct TreeNodeTableItem {
public int a;
public int b;
public int c;
public int d;
public int e;
}
private System.Collections.Generic.List<TreeNodeTableItem>
_tabularView;
....
_tabularView[i].a=amount1;
_tabularView[i].b=amount2;
_tabularView[i].c=amount3;
_tabularView[i].d=amount4;
_tabularView[i].e=amount5;
....
¿Does anybody how to factorize in a variable (or whatever else)
"_tabularView[i]"?
Thanks in advance
Date:Thu, 23 Aug 2007 02:45:08 -0700
Author:
|
Re: Efficient Array<> of valuetype entry manipulation
Francisco wrote:
> Hello,
>
> Is there any code faster than this array position manipulation (some
> code omitted for brevity)?:
>
> internal struct TreeNodeTableItem {
> public int a;
> public int b;
> public int c;
> public int d;
> public int e;
> }
>
> private System.Collections.Generic.List<TreeNodeTableItem>
> _tabularView;
>
> ...
> _tabularView[i].a=amount1;
> _tabularView[i].b=amount2;
> _tabularView[i].c=amount3;
> _tabularView[i].d=amount4;
> _tabularView[i].e=amount5;
> ...
>
> Does anybody how to factorize in a variable (or whatever else)
> "_tabularView[i]"?
TreeNodeTableItem tvi = _tabularView[i];
tvi.a = amount1;
tvi.b = amount2;
.. . . etc.
This saves an indexing step for each structure member access. Whether it's
noticeably faster for you depends on your usage, of course. This looks like a
pattern that a decent optimizer could recognize and handle for you, though.
HTH,
-rick-
Date:Thu, 23 Aug 2007 06:20:09 -0500
Author:
|
Re: Efficient Array<> of valuetype entry manipulation
Rick Lones wrote:
> TreeNodeTableItem tvi = _tabularView[i];
> tvi.a = amount1;
> tvi.b = amount2;
> .. . . etc.
>
> This saves an indexing step for each structure member access. Whether
> it's noticeably faster for you depends on your usage, of course. This
> looks like a pattern that a decent optimizer could recognize and handle
> for you, though.
Posting late always bites me. But I think the code you suggested is
_required_ (sort of), since the List<> type is a struct (value type).
That is, the indexer returns a copy of the value, not the indexed
element itself. So the only way to change the value within the list is
to initialize an existing value type variable with the desired values,
and then assign that to the indexed list item.
I wrote "sort of", because if one is overwriting all of the values in
the struct, obviously there's no point in retrieving the indexed list
item at the start (as in the assignment in the variable declaration in
the above code).
The code would make more sense as an _alternative_ rather than a
requirement if the list type was a class instead of a struct. Then the
value being returned by the indexer is the reference to the instance,
and the instance itself can be modified in-place without having to
reassign anything back to the list. And of course in that situation,
the assignment in the variable declaration is necessary.
Pete
Date:Thu, 23 Aug 2007 04:01:33 -0700
Author:
|
|
|