- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
问题背景
我读了this关于如何尽快反转字符串的问题。我发现答案之一是比较不同的方法。在其中一个中,他们只是运行一个循环,将位置 i
的元素与位置 string.Length-1-i
的元素交换,但他们通过 XOR 使用已知的棘手交换.我想知道与通过时间变量使用经典交换的相同方法相比,通过 XOR 使用交换来反转字符串的速度有多快。令人惊讶的是,与 XOR 相比,我的性能提高了近 50%。
问题
编译器是否在幕后做了一些神奇的事情,为什么我会得到这个结果?
使用基准修改后的代码
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ContestLibrary
{
public class Problem
{
delegate string StringDelegate(string s);
static void Benchmark(string description, StringDelegate d, int times, string text)
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (int j = 0; j < times; j++)
{
d(text);
}
sw.Stop();
Console.WriteLine("{0} Ticks {1} : called {2} times.", sw.ElapsedTicks, description, times);
}
public static string ReverseXor(string s)
{
char[] charArray = s.ToCharArray();
int len = s.Length - 1;
for (int i = 0; i < len; i++, len--)
{
charArray[i] ^= charArray[len];
charArray[len] ^= charArray[i];
charArray[i] ^= charArray[len];
}
return new string(charArray);
}
public static string ReverseClassic(string s)
{
char[] charArray = s.ToCharArray();
int len = s.Length-1;
for (int i = 0; i < len; i++, len--)
{
char temp = charArray[len];
charArray[len] = charArray[i];
charArray[i] = temp;
}
return new string(charArray);
}
public static string StringOfLength(int length)
{
Random random = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++)
{
sb.Append(Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65))));
}
return sb.ToString();
}
static void Main(string[] args)
{
int[] lengths = new int[] {1,10,100,1000, 10000, 100000};
foreach (int l in lengths)
{
int iterations = 10000;
string text = StringOfLength(l);
Benchmark(String.Format("Classic (Length: {0})", l), ReverseClassic, iterations, text);
Benchmark(String.Format("Xor (Length: {0})", l), ReverseXor, iterations, text);
Console.WriteLine();
}
Console.Read();
}
}
}
最佳答案
原因很简单,让我们看看每个函数在 IL 代码中有多少操作。但首先,让我们看看这两个函数在时间上的真正差异。你说 XOR 函数比另一个慢了近 50%,当我在 Debug模式下运行代码时我得到了那个结果,但是你必须在 Release模式下运行代码才能完全允许优化器完成它的工作:)。在 Release模式下,XOR 函数几乎慢了 3 倍。
图片具有 for 循环内部分的 IL 代码,这是唯一发生变化的部分。
第一张图是带有temp变量的函数
第二张图是异或函数
如您所见,指令数量的差异很大,14 对 34。3 倍的时间差异来自一些操作,例如 conv.u2,它们有点昂贵。
关于c# - 编译器使用经典交换对我的字符串反向方法进行了哪些优化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48180486/
本文实例总结了常用SQL语句优化技巧。分享给大家供大家参考,具体如下: 除了建立索引之外,保持良好的SQL语句编写习惯将会降低SQL性能问题发生。 ①通过变量的方式来设置参数 好:
写CSS的同学们往往会体会到,随着项目规模的增加,项目中的CSS代码也会越来越多,如果没有及时对CSS代码进行维护,CSS代码不断会越来越多。CSS代码交错复杂,像一张庞大的蜘蛛网分布在网站的各个位
所以我必须解决类的背包问题。到目前为止,我想出了以下内容。我的比较器是确定两个主题中哪一个是更好选择的函数(通过查看相应的(值,工作)元组)。 我决定迭代工作量小于 maxWork 的可能主题,并且为
前言:复杂类型说明 要了解指针,多多少少会出现一些比较复杂的类型,所以我先介绍一下如何完全理解一个复杂类型,要理解复杂类型其实很简单,一个类型里会出现很多运算符,他们也像普通的表达式一样,有优先级
代码如下: 复制代码代码如下: USE [tempdb] GO /****** Object: UserDefinedFunction [dbo].[fun
最近收到一个工作要求,让我完成一个每天一次的Linux服务器巡检工作(服务器的版本为红帽6.4),不可以使用监控软件来操作。在这里,把我的巡检过程和巡检脚本放送给大家做一参考。 首先,巡检内容
可以在 Classic ASP 中动态创建“空”对象并创建对象属性吗? 以这个 JavaScript 示例为例: var sample = new Object(); sample.prop = "O
我正在向旧的经典 asp 站点添加功能,但遇到了一个有趣的问题。页面上的以下行导致有用的错误“需要对象:''” strServerName = Request.ServerVariables("ser
我有一个经典的 ASP 应用程序,我正在处理日期截止。我的服务器位于中部时间,但我在东部时间。发生的情况是我的应用程序认为它早了一个小时,而我的截止时间晚了一个小时。我敢肯定,如果用户在太平洋时间,他
我是经典 ASP 的初学者。需要拆分一个由逗号分隔的许多电子邮件组成的字符串,并使用稍后生成的附加代码将结果插入(逐个电子邮件)到表格中。每条记录都应该有一个电子邮件地址。问题是我陷入了数组范围错误。
这个问题已经有答案了: one condition, multiple events (2 个回答) 已关闭 6 年前。 如何用更小的语句替换 1,2,3..。我尝试将 1 放入 10,但显示错误。
我是 ExtJS 的新手,所以我不知道是否可能。 Google 只回答如何为图表制作工具提示,所以... 我需要制作一个带有工具提示的网格,当用户将鼠标放在单元格上时将显示该工具提示。在该工具提示中,
我正在使用一个非常奇怪的 VB 版本...它不需要我告诉它什么是什么,它想自己弄清楚。 在 C# 中,我可以轻松地对数组进行硬编码...在 VB 中则不然。 我想在调用函数时创建一个硬编码数组...但
我的数据库访问代码如下: set recordset = Server.CReateObject("ADODB.Recordset") set cmd1 = Server.CreateObject(
我有 html 按钮和文本框的代码:文本框是我输入文本的地方,以便我可以在表格上进行一些更改。 'textbox " /> 'button Clear 我现在需要做的是单击“清除”按
我有一个表单,提交后会通过电子邮件发送。该表单使用 JavaScript 进行验证。经典 ASP 处理表单,即获取输入的数据、创建然后发送电子邮件。有报道称正在提交空白表格。仅发送标题和 Logo 。
加载页面 A.asp 默认情况下正在执行,从那里开始执行电子邮件的情况,如果是电子邮件,我们将调用页面 B。我想在控件被执行时执行相同的电子邮件情况从页面 B 转移到页面 A。请帮助我。 Page A
我正在使用经典 ASP 开发一个项目,例如,我想添加一些用户作为临时列表,当我提交表单时,这些数据将保存到数据库中。 我知道如何在 asp.net 中使用它,但不知道如何在经典 asp 中使用它。 例
我有一个带有简单 html 表的经典 ASP 页面,我想根据从数据库中提取的未知数量的记录循环表行,但是,当我使用 do/while 循环循环记录时,我收到一条错误消息,指出 Either BOF o
嘿,一直在寻找一段时间,但我似乎找不到任何有关如何在经典 asp 中处理日期的信息。 现在,我需要一种方法来计算今年过去的天数。我正在考虑一个简单的函数,它将获取当前日期,然后使用 (day = 1,
我是一名优秀的程序员,十分优秀!