473,386 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

Aug 23 '07 #1
8 1569
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-
Aug 23 '07 #2
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
Aug 23 '07 #3
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]"?

Thanks in advance
You can store the values in a local structure, and copy the structure to
the list:

TreeNodeTableItem item;
item.a = amount1;
item.b = amount2;
item.c = amount3;
item.d = amount4;
item.e = amount5;
_tabularView[i] = item;

The local structure is allocated on the stack, so populating it is very
efficient. The drawback is that the entire structure value is copied
another time to put it in the list.

Whether this is actually faster or not, is hard to tell, especially as
your structure is larger than the recommended 16 bytes.

--
Göran Andersson
_____
http://www.guffa.com
Aug 23 '07 #4
Rick Lones wrote:
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.
A small drawback is that it's not changing the value in the list at
all... ;)
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-

--
Göran Andersson
_____
http://www.guffa.com
Aug 23 '07 #5
Göran Andersson wrote:
Rick Lones wrote:
>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.

A small drawback is that it's not changing the value in the list at
all... ;)
Yikes! You are correct, of course. Now I don't know whether to laugh out loud
at myself or just go hide under the desk. But first I will make myself write
the code I should have run before posting . . .

Thank you Goran,
-rick-

using System;

namespace ConsoleApplication1
{
public class Ctest
{
public string c;
public Ctest(string init)
{
this.c = init;
}
}

public struct Stest
{
public string s;
public Stest(string init)
{
this.s = init;
}
}

class Class1
{
[STAThread]
static void Main()
{
Ctest c1 = new Ctest("c");
Ctest c2 = c1;

Stest s1 = new Stest("s");
Stest s2 = s1;

Console.WriteLine("Before: c1.c = " + c1.c + ", s1.s = " + s1.s);
c2.c = "c_new";
s2.s = "s_new";
Console.WriteLine("After: c1.c = " + c1.c + ", s1.s = " + s1.s);
Console.ReadLine();
}
}
}
Aug 24 '07 #6
Göran Andersson wrote:
>This saves an indexing step for each structure member access.

A small drawback is that it's not changing the value in the list at
all... ;)
In his defense, neither did the original code. :)
Aug 24 '07 #7
It appears that new generic implementations (List<here) doesn't
help with performance with structs as copying (instead of boxing) is
used when updating fields.

See http://www.interact-sw.co.uk/iangblo...7/modifyinsitu.

Aug 27 '07 #8
Francisco <fj********@arion.eswrote:
It appears that new generic implementations (List<here) doesn't
help with performance with structs as copying (instead of boxing) is
used when updating fields.

See http://www.interact-sw.co.uk/iangblo...7/modifyinsitu.
If you follow the frequently-given advice to avoid mutable structs,
it's not a problem anyway, of course.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 27 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Hal Vaughan | last post by:
If I have a byte and I convert it to string (String sData = new String(byte bData), then convert it back (byte bData = sData.getBytes()), will all data be intact, or do Strings have problems with...
1
by: herrcho | last post by:
#include <stdio.h> int multi; int main() { printf("\nmulti = %u",multi); printf("\nmulti = %u",multi); printf("\n&multi = %u\n",&multi); return 0;
9
by: Simple Simon | last post by:
Java longs are 8 bytes. I have a Java long that is coming in from the network, and that represents milliseconds since Epoch (Jan 1 1970 00:00:00). I'm having trouble understanding how to get it...
1
by: Michael Primeaux | last post by:
Why does the generic SortedList and generic SortedDictionary not define any virtual members? Thanks, Michael
20
by: martin-g | last post by:
Hi. Mostly I program in C++, and I'm not fluent in C# and .NET. In my last project I began to use LinkedList<and suddenly noticed that can't find a way to sort it. Does it mean I must implement...
1
by: kenneth6 | last post by:
1. In C++/MFC, if I pre-import the binary bitmap into the resource first, then how can I save the bitmap element values to 2D array as 1 and 0 ?? 2. In C++/MFC, If I have a 2D array containing 1...
22
by: amygdala | last post by:
Hi, I'm trying to grasp OOP to build an interface using class objects that lets me access database tables easily. You have probably seen this before, or maybe even built it yourself at some...
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
6
by: Bob Altman | last post by:
Hi all, I'm looking for the fastest way to convert an array of bytes to String. I also need to convert a String back to its original Byte() representation. Convert.ToBase64String and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.