假设有以下代码:
private static int DoSwitch(string arg)
{
switch (arg)
{
case "a": return 0;
case "b": return 1;
case "c": return 2;
case "d": return 3;
}
return -1;
}
private static Dictionary<string, Func<int>> dict = new Dictionary<string, Func<int>>
{
{"a", () => 0 },
{"b", () => 1 },
{"c", () => 2 },
{"d", () => 3 },
};
private static int DoDictionary(string arg)
{
return dict[arg]();
}
Run Code Online (Sandbox Code Playgroud)
通过迭代这两种方法并进行比较,即使"a","b","c","d"扩展为包含更多键,我也会得到字典稍快一些.为什么会这样?
这与圈复杂度有关吗?是因为抖动只将字典中的return语句编译为本机代码一次?是因为字典的查找是O(1),这可能不是switch语句的情况?(这些只是猜测)
我正在使用Reflector 查看Roslyn 2012年9月的CTP,我注意到SlidingTextWindow类具有以下内容:
internal sealed class SlidingTextWindow : IDisposable
{
private static readonly ConcurrentQueue<char[]> arrayPool = new ConcurrentQueue<char[]>();
private int basis;
private readonly LexerBaseCache cache;
private char[] characterWindow;
private int characterWindowCount;
private int characterWindowStart;
private int offset;
private readonly IText text;
private readonly int textEnd;
public SlidingTextWindow(IText text, LexerBaseCache cache)
{
this.text = text;
this.basis = 0;
this.characterWindowStart = 0;
this.offset = 0;
this.textEnd = text.Length;
this.cache = cache;
if (!arrayPool.TryDequeue(out this.characterWindow))
{
this.characterWindow = new char[2048];
} …Run Code Online (Sandbox Code Playgroud) 假设我有以下内容:
public class MyElement
{
}
[Serializable]
[StructLayout(LayoutKind.Sequential)]
struct ArrayElement
{
internal MyElement Element;
}
public class MyClass
{
internal MyElement ComputeElement(int index)
{
// This method does a lengthy computation.
// The actual return value is not so simple.
return new MyElement();
}
internal MyElement GetChild(ref MyElement element, int index)
{
if (element != null)
{
return element;
}
var elem = ComputeElement(index);
if (Interlocked.CompareExchange(ref element, elem, null) != null)
{
elem = element;
}
return elem;
}
} …Run Code Online (Sandbox Code Playgroud) 我正在使用Reflector 查看Roslyn 2012年9月的CTP,我注意到语法树的以下深度优先遍历:
private IEnumerable<CommonSyntaxNode> DescendantNodesOnly(TextSpan span,
Func<CommonSyntaxNode, bool> descendIntoChildren, bool includeSelf)
{
if (includeSelf && IsInSpan(span, FullSpan))
{
yield return this;
}
if ((descendIntoChildren != null) && !descendIntoChildren(this))
{
yield break;
}
var queue = new Queue<StrongBox<IEnumerator<CommonSyntaxNode>>>();
var stack = new Stack<StrongBox<IEnumerator<CommonSyntaxNode>>>();
stack.Push(new StrongBox<IEnumerator<CommonSyntaxNode>>(ChildNodes().GetEnumerator()));
while (stack.Count > 0)
{
var enumerator = stack.Peek();
StrongBox<IEnumerator<CommonSyntaxNode>> childEnumerator;
if (enumerator.Value.MoveNext())
{
var current = enumerator.Value.Current;
if (IsInSpan(span, current.FullSpan))
{
yield return current;
if ((descendIntoChildren == null) || descendIntoChildren(current))
{
childEnumerator …Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中使用Castle Windsor的控制反转.首次加载应用程序时,IWindsorContainer.Resolve将组件(特别是工厂)解析为实例.
例如,ILoggerFactory将其解析为MyCustomLoggerFactory(或实现的任何东西ILoggerFactory),其方法CreateLogger()创建记录器.我希望每次上课ILoggerFactory.CreateLogger().但是,我不想传入ILoggerFactory每个类的构造函数.
如何在ILoggerFactory不必IWindsorContainer.Resolve每次呼叫的情况下,每个班级如何访问特定工厂?
我有一个方法,其最后一个参数是params string[].我希望搜索一个程序集并使用params至少一个值来计算传递参数的用法数.
对这个方法有几百个调用,其中大多数都没有传递给最后一个params参数,所以使用像ReSharper的Find Usages这样的东西来计算那些没有传递给params参数的用法是不切实际的.
我如何使用反射/反汇编来做到这一点?或者,有没有可以做到这一点的工具?
我正在使用Reflector 查看Roslyn 2012年9月的CTP,我注意到ChildSyntaxList结构具有以下内容:
public struct ChildSyntaxList : IEnumerable<SyntaxNodeOrToken>
{
private readonly SyntaxNode node;
private readonly int count;
public Enumerator GetEnumerator()
{
return node == null ? new Enumerator() : new Enumerator(node, count);
}
IEnumerator<SyntaxNodeOrToken> IEnumerable<SyntaxNodeOrToken>.GetEnumerator()
{
return node == null
? SpecializedCollections.EmptyEnumerator<SyntaxNodeOrToken>()
: new EnumeratorImpl(node, count);
}
IEnumerator IEnumerable.GetEnumerator()
{
return node == null
? SpecializedCollections.EmptyEnumerator<SyntaxNodeOrToken>()
: new EnumeratorImpl(node, count);
}
public struct Enumerator
{
internal Enumerator(SyntaxNode node, int count)
{
/* logic */
}
public SyntaxNodeOrToken Current …Run Code Online (Sandbox Code Playgroud) 来自Richard Bird,Pearls of Functional Algorithm Design(2010),第6页:
对于纯函数编程器,更新操作采用数组大小的对数时间.公平地说,程序编程人员也理解,只有当数组很小时,才能进行恒定时间索引和更新.
在什么条件下数组有非恒定时间访问?这与CPU缓存有关吗?
可能重复:
解释用于设置,清除和测试单个位的算法
我有一个unsigned char.我希望将位2到4(从最低有效位计为0)复制到另一位unsigned char作为前三位.例如,在
abcdefgh // a,b,c,d,e,f,g,h are 0 or 1
Run Code Online (Sandbox Code Playgroud)
变
00000def
Run Code Online (Sandbox Code Playgroud)
我试过了
unsigned char input, output;
output = (input << 3) >> 5;
Run Code Online (Sandbox Code Playgroud)
这不起作用,但是
output = (input << 3)
output >>= 5;
Run Code Online (Sandbox Code Playgroud)
确实有效.
C中有没有办法在一行中完成这个任务?
我有两个表,它们要完全按原样并排放置。例如,
tableOne tableTwo
columnOne | columnTwo | columnThree columnI | columnII | columnIII
Run Code Online (Sandbox Code Playgroud)
两个表中的数据都不需要关联-这些表具有相同的行数-并且数据已经在两个表中进行了排序。基本上,我想full outer join在没有on运算符的情况下对两个表进行操作。
如何在SQL查询中执行此操作?
c# ×6
roslyn ×3
arrays ×2
c#-4.0 ×2
c ×1
caching ×1
clr ×1
dictionary ×1
il ×1
optimization ×1
params ×1
performance ×1
reflection ×1
sql ×1
sql-server ×1
value-type ×1