Unsafe Context : Pointer Unsafe « Language Basics « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Language Basics » Pointer UnsafeScreenshots 
Unsafe Context
Unsafe Context

/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson

Publisher: Apress  L.P.
ISBN: 1-893115-62-3
*/
// 36 - Deeper into C#\Unsafe Context
// copyright 2000 Eric Gunnerson
// file=unsafe.cs
// compile with: csc /unsafe /o+ unsafe.cs
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;


public class UnsafeContext
{
    const int iterations = 20000;    // # to do copy
    const int points = 1000;        // # of points in array
    const int retryCount = 5;        // # of times to retry
    
    public delegate Point[] CloneFunction(Point[] a);
    
    public static void TimeFunction(Point[] arr, 
    CloneFunction func, string label)
    {
        Point[]    arrCopy = null;
        long start;
        long delta;
        double min = 5000.0d;    // big number;
        
        // do the whole copy retryCount times, find fastest time
        for (int retry = 0; retry < retryCount; retry++)
        {
            start = Counter.Value;
            for (int iterate = 0; iterate < iterations; iterate++)
            arrCopy = func(arr);
            delta = Counter.Value - start;
            double result = (doubledelta / Counter.Frequency;
            if (result < min)
            min = result;
        }
    Console.WriteLine("{0}: {1:F3} seconds", label, min);
    }
    
    public static void Main()
    {
        Console.WriteLine("Points, Iterations: {0} {1}", points, iterations);
        Point[] arr = new Point[points];
        for (int index = 0; index < points; index++)
        arr[indexnew Point(35);
        
        TimeFunction(arr, 
        new CloneFunction(Point.ClonePointArrayMemcpy)"Memcpy");
        TimeFunction(arr, 
        new CloneFunction(Point.ClonePointArrayUnsafe)"Unsafe");
        TimeFunction(arr, 
        new CloneFunction(Point.ClonePointArray)"Baseline");
    }
}

class Counter 
{
    public static long Frequency 
    {
        get 
        {
            long freq = 0;
            QueryPerformanceFrequency(ref freq);
            return freq;
        }
    }
    public static long Value 
    {
        get 
        {
            long count = 0;
            QueryPerformanceCounter(ref count);
            return count;
        }
    }
    
    [System.Runtime.InteropServices.DllImport("KERNEL32",
    CharSet=System.Runtime.InteropServices.CharSet.Auto)]
    private static extern bool QueryPerformanceCounter(  ref long lpPerformanceCount);
    
    [System.Runtime.InteropServices.DllImport("KERNEL32"
    CharSet=System.Runtime.InteropServices.CharSet.Auto)]
    private static extern bool QueryPerformanceFrequencyref long lpFrequency);                     
}
public struct Point
{
    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
    
    // safe version
    public static Point[] ClonePointArray(Point[] a)
    {
        Point[] ret = new Point[a.Length];
        
        for (int index = 0; index < a.Length; index++)
        ret[index= a[index];
        
        return(ret);
    }
    
    // unsafe version using pointer arithmetic
    unsafe public static Point[] ClonePointArrayUnsafe(Point[] a)
    {
        Point[] ret = new Point[a.Length];
        
        // a and ret are pinned; they cannot be moved by
        // the garbage collector inside the fixed block.
        fixed (Point* src = a, dest = ret)
        {
            Point*    pSrc = src;
            Point*    pDest = dest;
            for (int index = 0; index < a.Length; index++)
            {
                *pDest = *pSrc;
                pSrc++;
                pDest++;
            }
        }
        
        return(ret);
    }
    // import CopyMemory from kernel32
    [DllImport("kernel32.dll")]
    unsafe public static extern void 
    CopyMemory(void* dest, void* src, int length);
    
    // unsafe version calling CopyMemory()
    unsafe public static Point[] ClonePointArrayMemcpy(Point[] a)
    {
        Point[] ret = new Point[a.Length];
        
        fixed (Point* src = a, dest = ret)
        {
            CopyMemory(dest, src, a.Length * sizeof(Point));
        }
        
        return(ret);
    }
    
    public override string ToString()
    {
        return(String.Format("({0}, {1})", x, y));
    }
    
    int x;
    int y;
}


           
       
Related examples in the same category
1.References and Pointers
2.Demonstrate pointers and unsafeDemonstrate pointers and unsafe
3.Demonstrate fixedDemonstrate fixed
4.Demonstrate the effects of pointer arithmethicDemonstrate the effects of pointer arithmethic
5.Demonstrate pointer comparisonDemonstrate pointer comparison
6.An array name with an index yields a pointer to the start of the arrayAn array name with an index yields a pointer to the 
   start of the array
7.Index a pointer as if it were an arrayIndex a pointer as if it were an array
8.Use fixed to get a pointer to the start of a stringUse fixed to get a pointer to the start of a string
9.Multiple IndirectMultiple Indirect
10.Unsafe code: copyUnsafe code: copy
11.Pointers and Declarative Pinning
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.