using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; /* * 106 * 93 * 91 * 102 * 91 * 100 * 100 * 97 * 98 * 95 * 平均=97.3 */ namespace ConsoleApplication1 { class Program { static void Main(string[] args) { foo a = new foo(); Stopwatch sw = new Stopwatch(); sw.Start(); a.start(); sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); } } class foo { public void start() { int value=0; for (long i = 0; i < 1000000; i++) { AddToBitfield(ref value, 5, 31); AddToBitfield(ref value, 5, 31); AddToBitfield(ref value, 5, 31); AddToBitfield(ref value, 5, 31); AddToBitfield(ref value, 5, 31); AddToBitfield(ref value, 5, 31); AddToBitfield(ref value, 5, 31); ReadFromBitfield(ref value, 5); ReadFromBitfield(ref value, 5); ReadFromBitfield(ref value, 5); ReadFromBitfield(ref value, 5); ReadFromBitfield(ref value, 5); } } void AddToBitfield(ref int bitfield, int bitCount, int value) { bitfield <<= bitCount; bitfield |= value; } int ReadFromBitfield(ref int bitfield, int bitCount) { int value = bitfield & ((1 << bitCount) - 1); bitfield >>= bitCount; return value; } } }