温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用指针优化性能

发布时间:2020-06-10 18:34:11 来源:网络 阅读:1289 作者:1473348968 栏目:编程语言

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

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

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数组,最简单的数组类型是逐个存储元素的内存块

使用指针优化性能

 

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI