Tag: 优化

C#XNA 2D轨迹效果优化

目前,作为我游戏中的一种线索效果,我每5帧就有一个精灵的半透明纹理副本被添加到一个List 的路径中。 这些轨迹的alpha值每帧递减,绘制函数遍历列表并绘制每个纹理。 一旦它们达到0 alpha,它们就会从List 中删除。 结果是移动实体背后的一个很好的小尾迹效果。 问题是大约100多个实体,帧速率开始急剧下降。 所有的跟踪纹理来自同一个精灵表,所以我不认为它是批处理问题。 我描述了代码,并且在FPS下降峰值期间CPU强度较低然后它处于正常FPS,所以我认为这意味着它的GPU限制? 有没有办法更有效地实现这种效果? 下面是使用的通用代码: // fade alpha m_alpha -= (int)(gameTime.ElapsedGameTime.TotalMilliseconds / 10.0f); // draw if (m_alpha > 0) { // p is used to alter RGB of the trails color (m_tint) depending on alpha value float p = (float)m_alpha/255.0f; Color blend = new Color((int)(m_tint.R*p), (int)(m_tint.G*p), (int)(m_tint.B*p), m_alpha); […]

从Texture2D创建UI图像

我正在开发一个实时流应用程序,它接收JPEG图像作为字节数组,并使用UI.Image其显示在屏幕上。 它工作正常,但我正在进行优化 ,几乎没有问题 。 目前,我在下面的代码将字节数组转换为Texture2D然后从Texture2D 创建一个Sprite ,然后将该Sprite分配给UI.Iamge以在屏幕上显示。 Texture2D camTexture; Image screenDisplay; public byte[] JPEG_VIDEO_STREAM; bool updateScreen = false; //初始化 JPEG_VIDEO_STREAM = new byte[20000]; camTexture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false); //在Update函数中运行的主代码 if(updateScreen){ camTexture.LoadImage(JPEG_VIDEO_STREAM); Sprite tempSprite = Sprite.Create(camTexture, new Rect(0, 0, camTexture.width, camTexture.height), Vector2.zero, 0); screenDisplay.sprite = tempSprite; updateScreen = false; } 上面的代码目前只执行3个步骤来显示图像到屏幕。 byte array – […]

更快计算项目出现的套数?

我有一个书签列表。 每个书签都有一个关键字列表(存储为HashSet)。 我还有一组所有可能的关键字(“宇宙”)。 我想找到大多数书签中出现的关键字。 我有1356个书签,总共有698,539个关键字,其中187,358个是唯一的。 如果我遍历Universe中的每个关键字并计算它出现的书签数量,我就会进行254,057,448次检查。 我的机器需要35秒。 算法非常简单: var biggest = universe.MaxBy(kw => bookmarks.Count(bm => bm.Keywords.Contains(kw))); 使用Jon Skeet的MaxBy 。 我不确定是否有可能加快这个速度,但有什么我可以做的吗? 也许以某种方式并行化它? dtb的解决方案需要不到200毫秒来构建宇宙并找到最大元素。 很简单。 var freq = new FreqDict(); foreach(var bm in bookmarks) { freq.Add(bm.Keywords); } var biggest2 = freq.MaxBy(kvp => kvp.Value); FreqDict只是我在Dictionary之上构建的一个小类。

多次使用属性时的性能考虑因素

我在使用string.format格式化我的字符串时使用CultureInfo.CurrentCulture 引用此博客 这只是暗示如果你经常使用CurrentCulture,可能值得将它读入私有变量,而不是大量调用CultureInfo.CurrentCulture,否则你将不必要地耗尽时钟周期。 所以这个作者 var culture = CultureInfo.CurrentCulture string.Format(culture,”{0} some format string”,”some args”); string.Format(culture,”{0} some format string”,”some other args”); 比…更好 string.Format(CultureInfo.CurrentCulture,”{0} some format string”,”some args”); string.Format(CultureInfo.CurrentCulture,”{0} some format string”,”some other args”); 根据MSDN, CultureInfo.CurrentCulture是一个属性 多次访问属性时是否存在性能损失? 我还做了一些经验分析,我的测试表明,使用局部变量比直接使用属性更昂贵。 Stopwatch watch = new Stopwatch(); int count = 100000000; watch.Start(); for(int i=0;i<count;i++) { string.Format(CultureInfo.CurrentCulture, "{0} is my name", "ram"); } […]

不必要的花括号会降低性能吗?

在编程之后最近遇到这个问题,我一直在想这个。 以下是两个合法和编译的片段。 具体来说,我的问题是这个..在第二种情况下,括号是否使程序变慢? 为什么允许这样做呢? 第一例: if (statement) { // do something } 第二个案例: { if (statement) { // do something } } 另外如果我有类似下面的代码的话。运行时是否与调用函数X相同而没有任何大括号。 { { { // call function X } } }

为什么按位运算符不像逻辑“和\或”运算符那样智能

我只是注意到按位操作不像逻辑“和\或”操作那样“智能”,我想知道为什么? 这是一个例子: // For the record private bool getTrue(){return true;} private bool getFalse(){return false;} // Since a is true it wont enter getFalse. bool a = getTrue() || getFalse(); // Since a is false it wont enter getTrue. bool b = getFalse() && getTrue(); // Since b is false it wont enter getTrue. b = […]

C#3.0参差不齐的数组性能优化与矩形数组相比如何?

在Apress的书“插图C#2008”中,pg。 343注意事项: “一维数组在CIL中有特定的指令,允许它们针对性能进行优化。矩形数组没有这些指令……因此,使用锯齿状数组有时会更有效……” 有谁知道如何实现这些性能优化?

如何快速使我的代码

任何人都可以指点我一个很好的教程,帮助我快速轻松地编写代码。 我有兴趣知道哪种方法更快,何时使用方法而不是其他… 以及如何评估代码是好还是坏? 我的编程语言是C#。 大家好, 感谢您的回复,他们非常有帮助。 我正在编辑我的问题,特别是优化是无限的。 我想知道每种情况下最好的方法是什么。 例如,如果我将字符串附加到字符串,使用StringBuilder优于字符串…我只需要这些简单的东西。

无法加载资源:403禁止使用.js优化

我正在尝试缩小我的.js和.css文件。 我已经安装了打包的Install-Package Microsoft.AspNet.Web.Optimization 什么时候我用BundleTable.EnableOptimizations = true;激活优化BundleTable.EnableOptimizations = true; 我在客户端收到此错误: 无法加载资源:服务器响应状态为403(禁止) http://localhost:22773/Content/themes/elevation/v=gnDLBbf1VVRuQDXtIYn1q0P3ICZG7oiwwgxPRbaLvqI1 任何人都知道我做错了什么? — BundleConfig info ——————————- public class BundleConfig { public static void RegisterBundles(BundleCollection bundles) { BundleTable.EnableOptimizations = true; bundles.Add(new ScriptBundle(“~/bundles/myJquery”).Include( “~/Scripts/jquery-1.9.1.js”, “~/Scripts/jquery-ui-1.10.1.custom.js”, “~/Scripts/jquery.signalR-1.0.1.js”, “~/Scripts/signalr-hubs.js”, “~/Scripts/Controls/Select/Simple/jquery.ui.selectmenu.js” )); bundles.Add(new ScriptBundle(“~/bundles/shared”).Include( “~/Scripts/global/prototypes.js”, “~/Scripts/global/mathutil.js”, “~/Scripts/global/elevationevents.js” )); bundles.Add(new ScriptBundle(“~/bundles/core”).Include( “~/Scripts/elevation/core/sys.config.js”, “~/Scripts/elevation/core/bays.js”, “~/Scripts/elevation/core/door.js”, “~/Scripts/elevation/core/horiziontal.js”, “~/Scripts/elevation/core/vertical.js”)); bundles.Add(new StyleBundle(“~/Content/themes/elevation”).Include( “~/Content/themes/dialogs/dialogs.css”, “~/Content/themes/social/ac/acSocial.css”, “~/Content/themes/elevation/elevation.css” […]

C#数组还是字典?

我想知道C#数组是否具有恒定的访问速度? 我需要在静态数组中存储1000个项目,这些项目将在服务器启动期间初始化。 此数组将以只读方式使用,因此不会对数组进行任何更改。 我应该使用简单的C#数组(新的MyClass [])或字典。 我是C#的新手,并试图了解C#数组访问是如何工作的。 它们可以通过速度与c ++数组进行比较吗?