首页 > 其他 > 详细

使用指针优化性能

时间:2014-10-26 19:51:27      阅读:267      评论:0      收藏:0      [点我收藏+]

============================创建基于栈的数组(高性能,低系统开销)

//数组的类型必须为值类型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
    class Program
    {
        static unsafe void Main(string[] args)
        {
            //stackalloc返回的地址的指针
            //分配的字节数=项数*sizeof(类型)
            int size = 20;//项数为20
            int* iarr = stackalloc int[size];
            for (int i = 0; i < size; i++)
            {
                //iarr[i] = i;      //这种模式也可以
                *(iarr + i) = i;
            }
            for (int j = 0; j < size; j++)
            {
                //Console.WriteLine(iarr[j]);      //这种模式也可以
                Console.WriteLine(*(iarr + j));
            }
            Console.ReadKey();
        }
    }
}

//如果给20个int数分配存储单元,就得到了一个有20个元素的int数组,最简单的数组类型是逐个存储元素的内存块

bubuko.com,布布扣

 

本文出自 “程序猿的家--Hunter” 博客,请务必保留此出处http://962410314.blog.51cto.com/7563109/1568205

使用指针优化性能

原文:http://962410314.blog.51cto.com/7563109/1568205

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!