Results 1 to 19 of 19

Thread: pointers in c#

  1. #1

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729

    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/

  2. #2
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

  3. #3
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    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.

  4. #4
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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...

  5. #5
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    Good points. Question:
    With pointers and unmanged code
    Isn't all of C# unmanaged? You are talking about C++ or something here, correct?

  6. #6

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    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/

  7. #7

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    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/

  8. #8
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    Thanks pt. You mean pointers in unsafe code, right?

  9. #9
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 .

  10. #10
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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.

  11. #11
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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.

  12. #12
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    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?

  13. #13
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I am not up on my compiler theory, but I bet it has to do something with the indexer and the CLR's optimizations.

  14. #14

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    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/

  15. #15
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    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.

  16. #16

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    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/

  17. #17

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    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/

  18. #18
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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.

  19. #19
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    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
  •  



Click Here to Expand Forum to Full Width