473,668 Members | 2,357 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Efficient Array<> of valuetype entry manipulation

Hello,

Is there any code faster than this array position manipulation (some
code omitted for brevity)?:

internal struct TreeNodeTableIt em {
public int a;
public int b;
public int c;
public int d;
public int e;
}

private System.Collecti ons.Generic.Lis t<TreeNodeTable Item>
_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)
"_tabularVi ew[i]"?

Thanks in advance

Aug 23 '07 #1
8 1577
Francisco wrote:
Hello,

Is there any code faster than this array position manipulation (some
code omitted for brevity)?:

internal struct TreeNodeTableIt em {
public int a;
public int b;
public int c;
public int d;
public int e;
}

private System.Collecti ons.Generic.Lis t<TreeNodeTable Item>
_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)
"_tabularVi ew[i]"?

TreeNodeTableIt em 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:
TreeNodeTableIt em 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 TreeNodeTableIt em {
public int a;
public int b;
public int c;
public int d;
public int e;
}

private System.Collecti ons.Generic.Lis t<TreeNodeTable Item>
_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)
"_tabularVi ew[i]"?

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

TreeNodeTableIt em 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 TreeNodeTableIt em {
public int a;
public int b;
public int c;
public int d;
public int e;
}

private System.Collecti ons.Generic.Lis t<TreeNodeTable Item>
_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)
"_tabularVie w[i]"?


TreeNodeTableIt em 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 TreeNodeTableIt em {
public int a;
public int b;
public int c;
public int d;
public int e;
}

private System.Collecti ons.Generic.Lis t<TreeNodeTable Item>
_tabularVie w;

...
_tabularVie w[i].a=amount1;
_tabularVie w[i].b=amount2;
_tabularVie w[i].c=amount3;
_tabularVie w[i].d=amount4;
_tabularVie w[i].e=amount5;
...

¿Does anybody how to factorize in a variable (or whatever else)
"_tabularVi ew[i]"?


TreeNodeTableI tem 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 ConsoleApplicat ion1
{
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.WriteLi ne("Before: c1.c = " + c1.c + ", s1.s = " + s1.s);
c2.c = "c_new";
s2.s = "s_new";
Console.WriteLi ne("After: c1.c = " + c1.c + ", s1.s = " + s1.s);
Console.ReadLin e();
}
}
}
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********@ari on.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.co m>
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
10300
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 bytes that are not printable characters? I've tested this and it seems to work fine, but I want to make sure there isn't some condition or situation I'm not aware of that could cause problems. I'm doing this because it's easier to do some of my...
1
1909
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
3674
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 into a struct timeval object. I get the ByteBuffer as an array of const unsigned char, 'buffer'. Here's an example: 00 00 01 0A 29 1D 07 E4 This value maps somehow to Thu Mar 23 16:57:49 2006 and some
1
4708
by: Michael Primeaux | last post by:
Why does the generic SortedList and generic SortedDictionary not define any virtual members? Thanks, Michael
20
20899
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 sorting for LinkedList<myself? Thanks in advance Martin
1
2333
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 and 0, how can I export the array to display a binary image on the windows dialog screen?? (Or where can I find the tuturial or examples about these two questions??)
22
3494
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 point in time. I have studied some examples I found on the internet, and am now trying to build my own from scratch. I have made a default table class (DB_Table), a default validation class (Validator) which is an object inside DB_Table and my...
45
18851
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 of their elements? Why can't I change the element itself? class Program { private struct MyStruct
6
5260
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 Convert.FromBase64String seem like the closest thing I can find to what I'm looking for baked into the base class library. Can anyone suggest a better way to do this? TIA - Bob
0
8459
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8374
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8791
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7398
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2018
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.