{"id":19612560,"url":"https://github.com/chenjd/performancetester","last_synced_at":"2026-05-15T15:32:37.640Z","repository":{"id":74015552,"uuid":"90518433","full_name":"chenjd/PerformanceTester","owner":"chenjd","description":"Unity相关操作的性能测试","archived":false,"fork":false,"pushed_at":"2017-05-10T12:45:05.000Z","size":26,"stargazers_count":1,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-26T17:46:42.452Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chenjd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-05-07T08:18:33.000Z","updated_at":"2017-08-08T07:41:07.000Z","dependencies_parsed_at":null,"dependency_job_id":"57587770-1c09-4792-b492-9d739a316f5c","html_url":"https://github.com/chenjd/PerformanceTester","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/chenjd/PerformanceTester","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chenjd%2FPerformanceTester","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chenjd%2FPerformanceTester/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chenjd%2FPerformanceTester/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chenjd%2FPerformanceTester/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chenjd","download_url":"https://codeload.github.com/chenjd/PerformanceTester/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chenjd%2FPerformanceTester/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33071111,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-15T11:35:32.926Z","status":"ssl_error","status_checked_at":"2026-05-15T11:35:31.362Z","response_time":103,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T10:47:03.917Z","updated_at":"2026-05-15T15:32:37.603Z","avatar_url":"https://github.com/chenjd.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PerformanceTester\nUnity相关操作的性能测试\n\n运行的结果如图（单位ms）：\n![QQ截图20170506163532.png](http://upload-images.jianshu.io/upload_images/1372105-3c297bd775b845f2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/800)\n我们可以发现在Unity 5.x版本中，泛型版本的GetComponent\u003c\u003e的性能最好，而GetComponent(string)的性能最差。\n\n做成柱状图可能更加直观：\n\n![QQ截图20170506163819.png](http://upload-images.jianshu.io/upload_images/1372105-ffb5c692cbc57d31.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/500)\n\n接下来，我们来测试一下我们感兴趣的堆内存分配吧。为了更好的观察，我们把测试代码放在Update中执行。\n\n    void Update()\n    {\n        for(int i = 0; i \u003c testCount; i++)\n        {\n            GetComponent\u003cTestComp\u003e();\n        }\n    }\n\n同样每帧执行1000000次的GetComponent\u003cT\u003e方法。打开profiler来观察一下堆内存分配吧：\n\n![QQ截图20170506204741.png](http://upload-images.jianshu.io/upload_images/1372105-8c57570a77b856c6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/800)\n我们可以发现，虽然**频繁调用GetComponent\u003cT\u003e时会造成CPU的开销很大，但是堆内存分配却是0B**。\n\n但是，我和朋友聊天偶尔聊到这个话题时，朋友说有时候会发现每次调用GetComponent\u003cT\u003e时，在profiler中都会增加0.5kb的堆内存分配。不知道各位读者是否有遇到过这个问题，那么是不是说GetComponent方法有时的确会造成GC呢？\n\n答案是否定的。\n\n这是因为朋友是在**Editor中运行**，并且**GetComponent\u003cT\u003e返回Null**的情况下，才会出现堆内存分配的问题。\n我们还可以继续我们的测试，这次把TestComp组件从场景中去除，同时把测试次数改为100000。我们在Editor运行测试，可以看到结果如下：\n![QQ图片20170506210207.png](http://upload-images.jianshu.io/upload_images/1372105-b49a039abae2ebfa.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1000)\n10000次调用GetComponent方法，并且返回为Null时，观察Editor的Profiler，可以发现每一帧都分配了5.6MB的堆内存。\n\n那么如果在移动平台上调用GetComponent方法，并且返回为Null时，是否会造成堆内存分配呢？\n\n这次我们让这个测试跑在一个小米4的手机上，连接profiler观察堆内存分配，结果如图：\n\n![QQ图片20170506212045.png](http://upload-images.jianshu.io/upload_images/1372105-9d3ead73a8a8d24d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)\n\n可以发现，在手机上并不会产生堆内存的分配。\n\n#### Null Check造成的困惑\n那么这是为什么呢？其实这种情况只会发生在运行在Editor的情况下，因为Editor会做更多的检测来保证正常运行。而这些堆内存的分配也是这种检测的结果，它会在找不到对应组件时在内部生成警告的字符串，从而造成了堆内存的分配。\n\u003e We do this in the editor only. This is why when you call GetComponent() to query for a component that doesn’t exist, that you see a C# memory allocation happening, because we are generating this custom warning string inside the newly allocated fake null object. \n\n所以各位不必担心使用GetComponent会造成额外的堆内存分配了。同时也可以发现只要不频繁的调用GetComponent方法，CPU的开销还是可以接受的。但是频繁的调用GetComponent会造成显著的CPU的开销的情况下，各位还是对组件进行缓存的好。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchenjd%2Fperformancetester","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchenjd%2Fperformancetester","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchenjd%2Fperformancetester/lists"}