对C#中的两个整数进行异或运算的最快方法是什么?

对C#中的两个整数进行异或运算的最快方法是什么?,c#,xor,C#,Xor,我需要对一个整数a与整数数组q进行异或运算(最大值为100000)。i、 e.如果我在循环,我会 异或q[0] 异或q[1] 异或q[100000] (10万次) 我将有一系列这样的a进行异或 我正在编写一个控制台应用程序,它将传递所需的输入 我正在使用内置的C#^操作符来执行异或操作。还有别的办法吗 将整数转换为字节数组,然后对每一位进行异或运算并计算出最终结果是一个好主意吗 输入(两行之间不要留空格) 一, 15 8 12345678910111313141415 106 10 10237

我需要对一个整数
a
与整数数组
q
进行异或运算(最大值为100000)。i、 e.如果我在循环,我会

异或q[0]

异或q[1]

异或q[100000]

(10万次)

我将有一系列这样的
a
进行异或

我正在编写一个控制台应用程序,它将传递所需的输入

我正在使用内置的C#
^
操作符来执行异或操作。还有别的办法吗

将整数转换为字节数组,然后对每一位进行异或运算并计算出最终结果是一个好主意吗

输入(两行之间不要留空格)

一,

15 8

12345678910111313141415

106 10

10237

33 5 8

182 5 10

181 113

5105

99 8 9

331014

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XOR
{
    class Solution
    {
        static void Main(string[] args)
        {

            List<TestCase> testCases = ReadLine();
            //Console.WriteLine(DateTime.Now.ToLongTimeString());
            CalculationManager calculationManager = new CalculationManager();

            foreach (var testCase in testCases)
            {
                var ints = testCase.Queries.AsParallel().Select(query => calculationManager.Calculate(query, testCase.SequenceOfIntegers)).ToList();
                ints.ForEach(Console.WriteLine);
            }

            //Console.WriteLine(DateTime.Now.ToLongTimeString());
            //Console.ReadLine();
        }

        private static List<TestCase> ReadLine()
        {
            int noOfTestCases = Convert.ToInt32(Console.ReadLine());
            var testCases = new List<TestCase>();

            for (int i = 0; i < noOfTestCases; i++)
            {
                string firstLine = Console.ReadLine();
                string[] firstLineSplit = firstLine.Split(' ');
                int N = Convert.ToInt32(firstLineSplit[0]);
                int Q = Convert.ToInt32(firstLineSplit[1]);

                var testCase = new TestCase
                                   {
                                       Queries = new List<Query>(),
                                       SequenceOfIntegers = ReadLineAndGetSequenceOfIntegers()
                                   };

                for (int j = 0; j < Q; j++)
                {
                    var buildQuery = ReadLineAndBuildQuery();
                    testCase.Queries.Add(buildQuery);
                }

                testCases.Add(testCase);
            }

            return testCases;
        }

        private static List<int> ReadLineAndGetSequenceOfIntegers()
        {
            string secondLine = Console.ReadLine();
            List<int> sequenceOfIntegers = secondLine.Split(' ').ToArray().Select(x => Convert.ToInt32(x)).ToList();
            return sequenceOfIntegers;
        }

        private static Query ReadLineAndBuildQuery()
        {
            var query = Console.ReadLine();
            List<int> queryIntegers = query.Split(' ').ToArray().Select(x => Convert.ToInt32(x)).ToList();
            Query buildQuery = ReadLineAndBuildQuery(queryIntegers[0], queryIntegers[1], queryIntegers[2]);
            return buildQuery;
        }

        private static Query ReadLineAndBuildQuery(int a, int p, int q)
        {
            return new Query { a = a, p = p, q = q };
        }


    }

    class CalculationManager
    {
        public int Calculate(Query query, List<int> sequenceOfIntegers)
        {
            var possibleIntegersToCalculate = FindPossibleIntegersToCalculate(sequenceOfIntegers, query.p, query.q);
            int maxXorValue = possibleIntegersToCalculate.AsParallel().Max(x => x ^ query.a);
            return maxXorValue;
        }

        private IEnumerable<int> FindPossibleIntegersToCalculate(List<int> sequenceOfIntegers, int p, int q)
        {
            return sequenceOfIntegers.GetRange(p - 1, (q - p) + 1).Distinct().ToArray();
        }
    }

    class TestCase
    {
        public List<int> SequenceOfIntegers { get; set; }
        public List<Query> Queries { get; set; }
    }

    class Query
    {
        public int a { get; set; }
        public int p { get; set; }
        public int q { get; set; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间异或
{
类解决方案
{
静态void Main(字符串[]参数)
{
列出testCases=ReadLine();
//Console.WriteLine(DateTime.Now.ToLongTimeString());
CalculationManager CalculationManager=新建CalculationManager();
foreach(testCases中的var testCase)
{
var ints=testCase.querys.AsParallel().Select(query=>calculationManager.Calculate(query,testCase.SequenceOfIntegers)).ToList();
内部ForEach(控制台写入线);
}
//Console.WriteLine(DateTime.Now.ToLongTimeString());
//Console.ReadLine();
}
私有静态列表ReadLine()
{
int noOfTestCases=Convert.ToInt32(Console.ReadLine());
var testCases=newlist();
对于(int i=0;iConvert.ToInt32(x)).ToList();
积分的返回序列;
}
私有静态查询ReadLineAndBuildQuery()
{
var query=Console.ReadLine();
List queryIntegers=query.Split(“”).ToArray().Select(x=>Convert.ToInt32(x)).ToList();
Query buildQuery=ReadLineAndBuildQuery(queryIntegers[0],queryIntegers[1],queryIntegers[2]);
返回buildQuery;
}
私有静态查询ReadLineAndBuildQuery(inta、intp、intq)
{
返回新查询{a=a,p=p,q=q};
}
}
类计算管理器
{
公共整数计算(查询、整数列表)
{
var-possibleIntegersToCalculate=FindPossibleIntegersToCalculate(整数序列,query.p,query.q);
int maxXorValue=possibleIntegersToCalculate.AsParallel().Max(x=>x^query.a);
返回maxXorValue;
}
私有IEnumerable FindPossibleIntegersToCalculate(整数序列列表,int p,int q)
{
返回integer.GetRange(p-1,(q-p)+1.Distinct().ToArray()的序列;
}
}
类测试用例
{
整数的公共列表序列{get;set;}
公共列表查询{get;set;}
}
类查询
{
公共int a{get;set;}
公共int p{get;set;}
公共int q{get;set;}
}
}

使用
^
逐位异或运算符是实现异或整数的最快方法

该操作被转换为单个原子处理器操作

正如您在拆解中看到的:

        int i = 4;
00000029  mov         dword ptr [ebp-3Ch],4 
        i ^= 3;
00000030  xor         dword ptr [ebp-3Ch],3 

因此,如果您希望让代码运行得更快,您应该更改算法/方法(如Marc Gravell所建议的),而不是xor方法。

我唯一愿意尝试的方法(如果有理由认为int方法太慢)是使用
不安全的
代码将每个
int[]
视为
长的*
,并且使用64位算术(同样,使用
^
)而不是32位,迭代次数减少一半,间接寻址减少一点。IIRC这和我对一些web套接字代码所做的差不多(为客户端到服务器的消息应用web套接字掩码是一个大容量异或)。显然,您需要注意最后几个字节。

如果您必须对数组执行称为a的更多元素的异或运算(如您所说),您可以通过以下方式加快运算速度:

int x = q[0]
for(int i = 1; i < q.Length; i++)
   x ^= q[i]

a1 ^= x
a2 ^= x
...
int x=q[0]
对于(int i=1;i
编辑: 对不起,基本上是相反的

int x = a1 ^ a2 ^ ... an
for(int i = 0; i < q.Length; i++)
     q[i] ^= x
intx=a1^a2^。。。一
for(int i=0;i
XOR是一种快速操作,因此您的应用程序将受到生成整数的速率的限制

如果您只是在它们可用时对其进行异或,那么无论采用何种方法,异或时间都是可以忽略的

e、 g.如果从文本文件中读取整数。磁盘io+解析时间将比xor时间大几个数量级。操作系统还将使用
预读
,这实际上意味着它将获取