Results 1 to 6 of 6

Thread: Of Pointers

Threaded View

  1. #6
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Of Pointers

    Quote Originally Posted by RohanWest
    Remember pointers cannot be used with managed reference types, structs are fine! I've used heaps of pointer arithmetic for image manipulation.
    You can use pointers on managed objects (classes) if you Marshal them properly. But thats another story.

    Mendhak makes a good point about the API, however you could and should use pointers when you need high-performance in general processing usage. I frequently use pointers when the built-in generalised functions are simply not fast enough. One prime example is the string.Split() function. Probably the most badly-abused functions in the framework.

    Lets say you have a large text file (tab-delimited flat file with 50 fields per line for arguments sake) and you want to extract fields 0, 8 and 12. If you used split (like 95% of programmers would) then you'd effectively be doing the following...

    1. Get a line from the file
    2. Allocating a dynamic array of dynamic-length strings.
    3. Then looping through the characters in the current line, and each time you find a tab character, copy the string into the array.
    4. Do whatever you need to with array[0], array[8] and array[12].
    5. Throw away the entire array.
    -- repeat 1 through 5 until the file is done.

    This is because Split is highly generic and therefore by definition unsuitable for most of the uses it is put to.

    Doing the same thing with pointers would do away with the arrays completely (instant massive memory and time saving). In total, you'd need probably 3 integers and 3 strings to store the fields you want to keep.

    The following image shows the process I have just described. The spikes labelled "S" denote the usae of Split() within the reading loop, those marked "P" denotes looping with pointers.

    The pointer version uses less than 1 percent of the RAM overhead of the split() method and is approximately 3.9 times faster.

    (Pointers took 335ms and Split() took 1354ms on average, to process a 50mb tab delimited file after loading the entire file into RAM first, 3 strings were extracted from each line of text. The output data of both methods was identical.

    So you see, pointers can be of huge benefit if used wisely.
    Attached Images Attached Images  
    Last edited by wossname; Mar 30th, 2007 at 06:50 AM.
    I don't live here any more.

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