|
-
Sep 10th, 2003, 05:19 AM
#1
Thread Starter
yay gay
pointers in c#
im doing a loop that does a lot of things and performance is critical so i though about using pointers in c#.
i have a big loop that is something like this:
Code:
for (int i=0;i < _dataSet.Tables["Files"].Rows.Count; i++) {
......code....
is it possible to use a pointer instead of i, and if yes would it be worth it? i dont understand much about pointers, how can i use them so i get faster speeds?
\m/  \m/
-
Sep 10th, 2003, 07:55 AM
#2
-
Sep 10th, 2003, 01:01 PM
#3
Frenzied Member
I don't think for what you are trying to do would be worth the trouble using pointers. They take up just as much memory as an integer because a pointer stores the address of the variable. I'm pretty sure that the address is an integer value.
If the variable was a double, it may be worth it.
-
Sep 10th, 2003, 02:59 PM
#4
PowerPoster
With pointers and unmanged code, he would see a slight increase because the CLR wouldn't need to look up the memory address of the variable in the look up table that stores the memory location. At least that is how I understand pointers.
The question would be how much performance he would gain.
First, you could probably get a little better performance by doing this:
Code:
int j = _dataSet.Tables["Files"].Rows.Count
for (int i=0;i < j; i++) {
...
}
This prevents the loop from having to traverse the object heiarchy everytime to find the Count. It only has to do it once to assign the value to j.
Next, how many rows are in this dataset? Why are you using a for loop instead of a foreach loop? You get a little better compiler optimization for using the foreach loop from what I hear.
Code:
foreach(DataRow dr in _dataSet.Tables["Files"].Rows){
...
}
Those are the questions you should be looking into. If this is a lengthly procedure, maybe you should look into putting it on its own thread and fire an event when done...
-
Sep 11th, 2003, 05:34 PM
#5
Frenzied Member
Good points. Question:
With pointers and unmanged code
Isn't all of C# unmanaged? You are talking about C++ or something here, correct?
-
Sep 11th, 2003, 05:54 PM
#6
Thread Starter
yay gay
hmm i am 99% sure the foreach loop wouldnt ever be faster than a for loop lol
about the
int j = _dataSet.Tables["Files"].Rows.Count
for (int i=0;i < j; i++) {
...
}
when i posted here the code i had it like i showed but later on i did the change to increase speed
about the rows in the dataset they are like 200 now but they can grow into bigger numbers. the problem is that this is like an offline file explorer thing and must work in a sequencial way. if u are inside a folder and want to go to another folder you must look over all the dataset and it must be as fast as possible so the user doesnt have to wait a lot
by the other side i never used pointers in c#(and only used them in test progs in c++ btw) so i found it could be a good real example to test them out!
\m/  \m/
-
Sep 11th, 2003, 05:58 PM
#7
Thread Starter
yay gay
Originally posted by aewarnick
Good points. Question:
Isn't all of C# unmanaged? You are talking about C++ or something here, correct?
c# is ALL managed but the pointers and the api stuff
\m/  \m/
-
Sep 11th, 2003, 06:02 PM
#8
Frenzied Member
Thanks pt. You mean pointers in unsafe code, right?
-
Sep 11th, 2003, 07:55 PM
#9
Sleep mode
Originally posted by aewarnick
You mean pointers in unsafe code, right?
Any data types , methods , code , or even keywords don't use CLR would be unsafe or unmanged . This means , it will not be managed by the CLR .
-
Sep 12th, 2003, 10:18 AM
#10
PowerPoster
Originally posted by PT Exorcist
hmm i am 99% sure the foreach loop wouldnt ever be faster than a for loop lol
Ok, you can laugh about it, but try it, then tell me what you think. I guess that 1% is where you need to be focusing on.
Code:
private void Form1_Load(object sender, System.EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
StringBuilder Buffer = new StringBuilder("");
for (int i = 1; i <= 1000; i++)
{
if (i > 1)
{
Buffer.Append(Environment.NewLine);
}
Buffer.Append("This is line number " + i.ToString());
}
TheRichTextBox.Text = Buffer.ToString();
Cursor.Current = Cursors.Arrow;
}
private void ForLoopButton_Click(object sender, System.EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
int Len = 0;
int Start = Environment.TickCount;
// Unoptimized loop:
// for (int i = 0; i < TheRichTextBox.Lines.Length; i++)
// {
// Len += TheRichTextBox.Lines[i].Length;
// }
// Optimized to not have to look at a property each time
int count = TheRichTextBox.Lines.Length;
for (int i = 0; i < count; i++)
{
Len += TheRichTextBox.Lines[i].Length;
}
int ElapsedTime = Environment.TickCount - Start;
ResultsTextBox.Clear();
ResultsTextBox.Text = "for loop\r\n\r\nElapsed time = " + ((double) ElapsedTime / (double) 1000.0).ToString() + " seconds\r\n\r\nResult = " + Len.ToString();
Cursor.Current = Cursors.Arrow;
}
private void ForEachLoopButton_Click(object sender, System.EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
int Len = 0;
int Start = Environment.TickCount;
foreach (String Line in TheRichTextBox.Lines)
{
Len += Line.Length;
}
int ElapsedTime = Environment.TickCount - Start;
ResultsTextBox.Clear();
ResultsTextBox.Text = "foreach loop\r\n\r\nElapsed time = " + ((double) ElapsedTime / (double) 1000.0).ToString() + " seconds\r\n\r\nResult = " + Len.ToString();
Cursor.Current = Cursors.Arrow;
}
I pulled this code from another site, and added to it a little. Here is what he said about it:
Using a foreach loop will be substantially faster than a for loop when accessing the items in a RichTextBox's Lines property. For example, the following code loads a RichTextBox with 1000 lines of text and accesses each line from the Lines property. On a 333 MHz Pentium II machine, the for-loop code (ForLoopButton_Click) takes ~25 seconds and the foreach code (ForEachLoopButton_Click) takes ~0.01 seconds.
-
Sep 12th, 2003, 10:23 AM
#11
PowerPoster
Oh ya, to add to that last post of mine, I am getting a 2.5 second difference on a 2ghz with 512MB or ram machine. That is huge when you are talking about UI work.
-
Sep 12th, 2003, 12:52 PM
#12
Frenzied Member
When I wrote this
Isn't all of C# unmanaged?
I meant managed.
Moving on...
Interesting. The foreach loop, faster! Any explanation as to why?
-
Sep 12th, 2003, 01:45 PM
#13
PowerPoster
I am not up on my compiler theory, but I bet it has to do something with the indexer and the CLR's optimizations.
-
Sep 12th, 2003, 01:48 PM
#14
Thread Starter
yay gay
hmmmmm now that you talk about that i think many time ago i've read something like that too..its funny
and about pointers? with pointers they have any tests to compare with? in that example how could one use pointers?
tks
\m/  \m/
-
Sep 12th, 2003, 01:58 PM
#15
PowerPoster
I'm not to sure why you are so interested in pointers. I've watched many msdn interviews with well-known authors in the .NET community, and I would say 99% of them say they have not ran into a situation where they needed to work with them. The CLR is optimized for maximum performance, so I really wouldn't worry about it in your scenario. I would more so work on fine tunning your sql statements or store procedures (and your app infrastructure). These are the real issues you need to concern yourself with.
Last edited by Lethal; Sep 12th, 2003 at 02:12 PM.
-
Sep 12th, 2003, 04:27 PM
#16
Thread Starter
yay gay
yeah lethal like i said before i'd like to use them like a way of testing..i've never really used them
and i think its interesting to compare the results with results not using pointers
\m/  \m/
-
Sep 12th, 2003, 04:35 PM
#17
Thread Starter
yay gay
and im not very into the subject but as i am using XML i dont think i can use stored procedures etc
other reason i believe pointers arent being used much its because .net its only the begginig and there arent still that much ppl interested in them, you'll see that within sometimes more threads about pointers will appear in this forum...with ppl getting more knowlodge about basic things of .net they will want to know how to work too with pointers and even using them in real apps.
thats what i think at least.
\m/  \m/
-
Sep 12th, 2003, 04:49 PM
#18
PowerPoster
Originally posted by PT Exorcist
and im not very into the subject but as i am using XML i dont think i can use stored procedures etc
other reason i believe pointers arent being used much its because .net its only the begginig and there arent still that much ppl interested in them, you'll see that within sometimes more threads about pointers will appear in this forum...with ppl getting more knowlodge about basic things of .net they will want to know how to work too with pointers and even using them in real apps.
thats what i think at least.
I think you are wrong. Most people, probably around the 99% mark, that use C# won't even touch pointers. This is one of the huge benifits of C#. If you need lightning speed on something, you should be using a language that allows lightning speed to happen without jumping through hoops to get it. C++ would come to mind. Then, call your C++ code from C# that way you can do all the other stuff that doesn't require that speed easily.
C# was designed so you don't need pointers. Sure, they allow you to use them, but they are not recommended because when you start using them, you eliminate all the pluses to the language.
Just my opinion though. If more speed is what you require than what C# can provide you, C++ or Assembly should be what you are looking at.
-
Sep 12th, 2003, 05:22 PM
#19
PowerPoster
Originally posted by hellswraith
I think you are wrong. Most people, probably around the 99% mark, that use C# won't even touch pointers. This is one of the huge benifits of C#. If you need lightning speed on something, you should be using a language that allows lightning speed to happen without jumping through hoops to get it. C++ would come to mind. Then, call your C++ code from C# that way you can do all the other stuff that doesn't require that speed easily.
C# was designed so you don't need pointers. Sure, they allow you to use them, but they are not recommended because when you start using them, you eliminate all the pluses to the language.
Just my opinion though. If more speed is what you require than what C# can provide you, C++ or Assembly should be what you are looking at.
My thoughts exactly..
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
|