473,377 Members | 1,151 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,377 software developers and data experts.

Static, ReadOnly/Const array in a method

Hi all !

Considering the code below, is it possible to declare this array
static and readonly/const in order to force .NET to allocate this
array only once instead of at each method call. Thank you for your
help !

public static int NbBitsOnOpt(int n)
{
byte[] PreComputedNbBitsOn = new byte[] {0, 1, 1, 2, 1, 2,
2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2,
3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4,
3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5,
6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3,
3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3,
4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6,
4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3,
4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5,
5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3,
4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4,
3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5,
6, 4, 5, 5, 6, 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5,
5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6,
7, 7, 8 };
const int NbBits = sizeof(int);
int nbBitsOn = 0;

for (int i = 0; i < NbBits; i++)
{
nbBitsOn += PreComputedNbBitsOn[n & 0xFF];
n >>= 8;
}

return nbBitsOn;
}

May 15 '07 #1
4 2740
On Tue, 15 May 2007 10:18:10 -0700, Martin <ma*********@gmail.comwrote:
Considering the code below, is it possible to declare this array
static and readonly/const in order to force .NET to allocate this
array only once instead of at each method call. Thank you for your
help !
Well, for sure you could make it a static field outside your method.

That said, are we allowed to comment on your algorithm? :) You'd have to
be spending a LOT of time counting bits for your "always four iteration"
loop to produce a measurable performance improvement over the
less-data-centric "on average, sixteen iteration" algorithm, or even the
brute-force "always thirty-two iteration" version.

Pete
May 15 '07 #2
Hi Peter,

I agree with you, in that case, there is not a huge performance
improvement. There is an improvement only if the method is called many
time and the array of precomputed data is static and outside the
method. Do you have a better suggestion for counting bits? :D

Martin

May 15 '07 #3
Martin <ma*********@gmail.comwrote:
I agree with you, in that case, there is not a huge performance
improvement. There is an improvement only if the method is called many
time and the array of precomputed data is static and outside the
method. Do you have a better suggestion for counting bits? :D
I'd suggest using the simplest possible way (maybe bit shifting until
it's zero, being careful of negative numbers) until you run into a
situation where the performance is a bottleneck.

See http://infolab.stanford.edu/~manku/b.../bitcount.html for a
page on this though.

--
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
May 15 '07 #4
On Tue, 15 May 2007 11:32:44 -0700, Martin <ma*********@gmail.comwrote:
I agree with you, in that case, there is not a huge performance
improvement. There is an improvement only if the method is called many
time and the array of precomputed data is static and outside the
method. Do you have a better suggestion for counting bits? :D
Sure. Though, the page that Jon posted the link to includes the two
options I would have offered, plus more, so I won't bother writing them
here. :)

I would recommend, absent any specific performance problems, that you just
do the brute-force, shift one bit at a time, incrementing a counter each
time you find a bit set ("Iterated Count" in the linked page). It's
simple, readable, gets the job done, and assuming your program is mostly
doing other things, you will never notice the performance difference.

If you do, by some strange turn of events, discover that you are spending
a significant amount of time in your program counting bits, my next step
would be to look at whether that's really what you meant to do. That is,
unless the work your program does is inherently about counting bits, I'd
expect that spending a lot of time counting bits means that you're
probably just counting bits more often than you really need to. Reworking
the algorithm so that you really only count bits when you have to is
likely to fix that performance issue.

Finally, if after all that analysis, you decide that yes, you really need
to be counting bits as part of a major component of your processing, then
you go with the table-driven lookup method. Given how much RAM you can
have these days, you might even consider using a 16-bit table instead of
an 8-bit one. It'll double your performance (after you initialize the
table...you should probably just write a loop that initializes the table
by brute force at the beginning of your program, rather than hard-coding
all the values), and will still only take up 64K (small potatoes these
days). I doubt it would come to that though. :)

Pete
May 15 '07 #5

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

Similar topics

1
by: John David Ratliff | last post by:
I'm new to C++ and have a question about static class constants. Let's say we have two classes --------------------------------------------- #include <iostream> using namespace std; ...
5
by: Kavitha | last post by:
Hi Pals, I am confused between const and static readonly in C#. I want to know the difference between then. Please give me clear explanation. thanks in advance. -- Kavitha
12
by: Sergey Klementiev | last post by:
Why it's impossible to have a static indexer in C#?
5
by: Vladimir Bezugliy | last post by:
Why I can't declare a member of class as "static const". As far as I see I should declare as "static readonly". class StateObject { public static const int BufferSize = 1024; } ...
19
by: cody | last post by:
Iam wondering what the benefit of using const over static readonly is. static readonly is a runtime constant and can be set once in the initializer or the static ctor, whereas const is suffering...
5
by: cody | last post by:
I know I can use ArrayList.ReadOnly() to create a readonly version of it, but how can I achive a similar thing with a generic collection of .NET 2.0?
5
by: Sek | last post by:
hi folks, i have a bunch of strings used in my code in many places. these strings reside inside a instantiable class. so, i want to replace these with constant/static variable to control the...
10
by: sunil | last post by:
Hello, I am new to c# . I have some basic programming doubts. Please help me in clarifying these doubts. I want to initialize a static and readonly field with a value returned by a static...
7
by: JPS | last post by:
Can someone please refresh my memory here, but what is the difference between a Static variable (or method) and a Const?
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.