|
-
Oct 15th, 2008, 02:02 PM
#1
Too many functions!
Original article
As one of the more experienced C# coders in his group, Yakir is often asked programming questions. Recently, his colleague James asked him the best way to store hundreds of thousands of items in memory, to which Yakir replied "It depends on how you want to access your data. If you want to access your data by index, you should store it in an ArrayList. If its easier to store things as a key-value pair, then you should use a Hashtable."
James' particular application called for a Hashtable, so he decided to implement that. A few hours later, however, he ran into some trouble. "This Hashtable isn't really working," James explained, "do you know if there's something more efficient?"
"Errr," Yakir questioned, "there isn't really anything more efficient than a Hashtable. What exactly is the problem?"
"It's just too slow," James replied, "once I have real data in there, it takes almost five seconds to add, remove, or find, an item."
Yakir knew something was definitely wrong and sat with James to look at his code. Here's what he saw...
Code:
class HashTable
{
public object[] keys;
public object[] values;
public HashTable()
{
keys = new object[0];
values = new object[0];
}
public void Add(object key, object value)
{
Array.Resize(ref keys, keys.Length + 1);
Array.Resize(ref values, values.Length + 1);
keys[keys.Length - 1] = key;
values[values.Length - 1] = value;
}
public void Remove(object key)
{
object[] tempKeys = new object[0];
object[] tempValues = new object[0];
for (int i = 0; i <= keys.Length - 1; i++)
{
if (!keys[i].Equals(key))
{
Array.Resize(ref tempKeys, tempKeys.Length + 1);
Array.Resize(ref tempValues, tempValues.Length + 1);
tempKeys[tempKeys.Length - 1] = keys[i];
tempValues[tempValues.Length - 1] = values[i];
}
}
keys = tempKeys;
values = tempValues;
}
public object GetItem(object key)
{
for (int i = 0; i <= keys.Length - 1; i++)
{
if (keys[i].Equals(key))
{
return values[i];
}
}
return null;
}
public int NumberOfItems
{
get
{
return keys.Length;
}
}
}
"Uhhh," Yakir said, baffled. "Why didn't you use the built-in Hashtable class from System.Collections that I showed you?"
"I checked it out," James explained, "but it had too many functions, which means it's slow. My class only has 3 functions, so it's much more efficient."
-
Oct 15th, 2008, 02:15 PM
#2
-
Oct 15th, 2008, 02:19 PM
#3
Re: Too many functions!
Can anybody actually see the link in that post?
My usual boring signature: Nothing
 
-
Oct 15th, 2008, 02:23 PM
#4
Re: Too many functions!
Well, no, since it contains a word that VBF's nanny-ware regards as slightly rude.
Replacing the three asterisks in the URL with "doubleyou tee eff" seems to yield a post remarkably similar to the drivel that kasracer wasted all our time with.
I don't live here any more.
-
Oct 15th, 2008, 02:26 PM
#5
Re: Too many functions!
crap
No original article for you then.
Daily Whiskey Tango Foxtrot DOT Com
-
Oct 15th, 2008, 02:27 PM
#6
Re: Too many functions!
Oh good. For a moment there I began to suspect QWest of being as incompetent as usual. I'm too lazy to look up Kleinma's post about the nefarious ISP that was redirecting some types of errors to their custom search engine, but QWest has just started doing that, too. This was the second post in 24 hours with a link that took me to their search engine, and I was beginning to wonder if they had just decided to re-direct pretty nearly anything. They really are laughably inept, but the errors have been mostly in my favor, so far.
My usual boring signature: Nothing
 
-
Oct 15th, 2008, 11:59 PM
#7
Hyperactive Member
Re: Too many functions!
 Originally Posted by Article
it had too many functions, which means it's slow. My class only has 3 functions, so it's much more efficient.
At last! Someone that agrees with me!!!
-
Oct 16th, 2008, 01:09 AM
#8
Re: Too many functions!
I name all my functions with a prefix 'static' because that makes it faster.
Code:
public int StaticGetEmployeeSalary(int employeeID)
{
//...
}
-
Oct 16th, 2008, 05:17 AM
#9
Frenzied Member
Re: Too many functions!
If you prefix them with virtual they are faster again because they are not really there....
-
Oct 16th, 2008, 05:23 AM
#10
Fanatic Member
Re: Too many functions!
 Originally Posted by BillGeek
At last! Someone that agrees with me!!!
-
Oct 16th, 2008, 06:06 AM
#11
Re: Too many functions!
HTML is faster than C! Plus it's OOP too!
I don't live here any more.
-
Oct 16th, 2008, 06:08 AM
#12
Frenzied Member
Re: Too many functions!
Everytime you read HTML god kills a kitten. Just be warned.
-
Oct 16th, 2008, 07:49 AM
#13
Fanatic Member
Re: Too many functions!
 Originally Posted by DeanMc
Everytime you read HTML god kills a kitten. Just be warned.
That must be a lot of kittens then. Are you trying to say that Icanhascheezburger.com just recycles the 3 cats over and over again??
-
Oct 16th, 2008, 07:57 AM
#14
Re: Too many functions!
HTML is faster than C! Plus it's OOP too!
I recently read a CV where the applicant had listed one of his skills as OOPS.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Oct 16th, 2008, 08:09 AM
#15
Re: Too many functions!
 Originally Posted by mendhak
I name all my functions with a prefix 'static' because that makes it faster.
Code:
public int StaticGetEmployeeSalary(int employeeID)
{
//...
}
I finish all my functions with DoCommand.RunFaster..... and it works!
-tg
-
Oct 16th, 2008, 08:09 AM
#16
Re: Too many functions!
 Originally Posted by DeanMc
If you prefix them with virtual they are faster again because they are not really there....
Only when being used to write vaporware.
-tg
-
Oct 20th, 2008, 05:41 AM
#17
Re: Too many functions!
When I want to hide a function I use:
Code:
void _______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________HiddenFunction(void) {
....
}
-
Oct 20th, 2008, 07:52 AM
#18
Re: Too many functions!
you could also try applying the <nopeeking> attribute to it as well.... as does this attribute:
<jedi-mind-trick("This is not the function you are looking for.")>
-tg
-
Oct 21st, 2008, 07:52 PM
#19
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|