|
Luc Pattyn wrote: straight table lookup
Or a switch?
switch ( x )
{
case 255 :
...
case 128 : return 8 ; break ;
...
}
No, no, it's just a joke, really, I'm not suggesting it. Put the noose away!
|
|
|
|
|
PIEBALDconsult wrote: Put the noose away!
Had to look that up before I knew what it was I was to discard.
On classic architectures a switch statement constitutes the biggest semantic gap between a
programming language and the underlying hardware. And there is almost no compiler that
even comes close to the optimal code.
OTOH on a MicroChip PIC you can do a "computed goto" and stuff a code block with "return const"
instructions, that is basically a switch that implements the cheapest table lookup!
So your suggestion would fit nicely there (when coded in assembly that is).
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Yikes.
You basically took method 3 and added some casts and some method calls.
How would this be faster?
Recursion is a valuable concept, often misused, it always has a cost associated with it.
In your creation it is not quite recursion, each nesting level needs a different method,
and to top it off, you have choosen to confuse the reader by giving them all the same
name.
BTW: MSB traditionally stands for the most significant byte or bit (the one that holds the
sign in signed integers), independent of its value, and not the highest bit set.
Example: "RS232C transmits its data LSB-first".
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Luc Pattyn wrote: How would this be faster?
Well if given a byte, ushort, or uint it wouldn't need to convert them to ulong before beginning.
Luc Pattyn wrote: you have chosen to confuse the reader by giving them all the same name.
Of course; they all do the same thing.
Luc Pattyn wrote: not the highest bit set.
Fine I'll rename them.
This is why I tend to post answers to Friday programming quizzes on Sunday evening; I go through a lot of false starts. I need a head start.
|
|
|
|
|
PIEBALDconsult wrote: Well if given a byte, ushort, or uint it wouldn't need to convert them to ulong before beginning
But now each call contains a cast??
PIEBALDconsult wrote: Of course; they all do the same thing
I agree, at least in principle, but here it needs careful attention to see which is
calling which, and I am very much in favor of readability; since they would be private
methods, I think I would give them related but clearly different names.
PIEBALDconsult wrote: I need a head start
Which friday we will start has not yet been decided, has it?
Anyway, I still have some extreme ideas that will take some time to implement...
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Luc Pattyn wrote: since they would be private
methods
But they wouldn't be.
|
|
|
|
|
Something like this?
System.Math.Floor ( System.Math.Log ( val , 2 ) )
|
|
|
|
|
I just timed this method vs. the algorithm I initially posted, as well as a new faster alg. using a binary search scheme (see my response to the post before yours), and the algorithm that you proposed is OVER 4.5 times slower than my fast method (~3 times slower than my slow method for big integers)! That is why I am attempting to devise my own integer algorithm. The double arithmetic involved in the computation is EXTREMELY expensive and unneccessary, and the slowdown is unacceptable for my application.
Jeff
|
|
|
|
|
Hi Jeff,
Skippums wrote: Is there a better way to do this, like perhaps a binary search algorithm?
yes there is.
Seems you want the bit number of the most significant bit set.
Some CPUs have an instruction for this ("find first one" or "count leading zeroes").
Using it on x86 would mean P/Invoke to another dll (C/C++ with asm)
Without asm, the optimal approach depends on the application. So here are a few questions:
What is the range of integers you expect? (smaller range may yield faster code)
And what is the distribution? (to optimally order the operations)
final Q: is there any calculation involved in getting the input value (such as multiplying,
dividing, shifting)?
BTW: the Petes and Pauls of CP will tell you "wrong forum; try Math/Algo".
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
The input value is the size of a file, in bytes. It is provided by the client to my application (we assume they are good clients and provide the info correctly). Therefore, I expect the range to be anywhere between 1,000 (1 kB) and 1,000,000,000 (1 GB). I think the fastest I can get from C# code is as noted in my response to the first person who answered my question. I would be interested to try actually writing the asm code, and importing it into my C# project to get better performance. That sounds like a fun exercise. I will post the results of it as soon as I try it!
Jeff
|
|
|
|
|
Let me know if you get assembly in C#.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
Luc Pattyn wrote: Seems you want the bit number of the most significant bit set.
That may be what he wants, but that's not what he asked for.
Hmmm, maybe it could be done recursively...
|
|
|
|
|
Yeah, those software guys often use a strange language to ask a simple question.
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Sorry... I figured that nobody who started programing in C# would know what a "bit" is
Jeff
|
|
|
|
|
No, that's the VB folk. But they don't know log or base either.
|
|
|
|
|
Unless they tried to use the BitArray class...
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
what are all the features of .net
|
|
|
|
|
Moron Alert....
Regards,
Rob Philpott.
|
|
|
|
|
Are you being serious? You think someone is going to list ALL the features of .net in a forum post?
|
|
|
|
|
That's really too broad a question to answer in this forum. Do some research[^].
|
|
|
|
|
Judah Himango wrote: Do some research[^].
Reading is for sissies! Real programmers never stop typing code, never stop to read, never stop to think.
|
|
|
|
|
|
Well it begins with a "." (dot), followed by an "n", then an "e", and finally a "t".
|
|
|
|
|
Is that dots are primarily a very small fish. Thus the wholes for a dot net need to be really small. This causes more resistance when dragged through the water, however. So when considering the purchase of a dot net look at the rigidity factor of the pole, the UV resistance of the netting, the size of the holes in the netting, as well as the inverse viscosity measure.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
1. georgemathew2@hotmail.com View details
Status Bronze. Member No. 4493066
View Member's Blog.
Awards
Messages Posted
6 - Poster
Articles Submitted
0 - Browser
Biography I am a biggner and I am focused on C# and Asp.Net
Birthday Sunday 21st February, 1982
Location India
Occupation Software development
Interests COM, C#, ASP.NET, VB.NET, Javascript
Member since Friday 7th September, 2007
(2 months)
"If an Indian asked a programming question in the forest, would it still be urgent?" - John Simmons / outlaw programmer
I get all the news I need from the weather report - Paul Simon (from "The Only Living Boy in New York")
|
|
|
|