这是一段看似非常特殊的C++代码.出于某种奇怪的原因,奇迹般地对数据进行排序使得代码几乎快了六倍.
#include <algorithm>
#include <ctime>
#include <iostream>
int main()
{
// Generate data
const unsigned arraySize = 32768;
int data[arraySize];
for (unsigned c = 0; c < arraySize; ++c)
data[c] = std::rand() % 256;
// !!! With this, the next loop runs faster.
std::sort(data, data + arraySize);
// Test
clock_t start = clock();
long long sum = 0;
for (unsigned i = 0; i < 100000; ++i)
{
// Primary loop
for (unsigned c = 0; c < arraySize; ++c) …Run Code Online (Sandbox Code Playgroud) public sealed class Singleton
{
Singleton() {}
public static Singleton Instance
{
get
{
return Nested.instance;
}
}
class Nested
{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Nested() {}
internal static readonly Singleton instance = new Singleton();
}
}
Run Code Online (Sandbox Code Playgroud)
我希望在我目前的C#应用程序中实现Jon Skeet的Singleton模式.
我对代码有两个疑问
如何访问嵌套类中的外部类?我的意思是
internal static readonly Singleton instance = new Singleton();
Run Code Online (Sandbox Code Playgroud)
有什么叫封闭吗?
我无法理解这个评论
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit …Run Code Online (Sandbox Code Playgroud)大多数(如果不是全部)我的实体框架POCO都具有虚拟功能.我需要这些函数是虚拟的,以便实体可以延迟加载.
如果我Accommodations在构造函数中初始化,那么我将在构造函数中调用虚函数,这是不好的做法.
但是Accommodations如果不在构造函数中,我怎么能初始化?
public class Venue
{
public Venue()
{
Accommodations = new HashSet<Accommodation>();
}
public virtual ICollection<Accommodation> Accommodations { get; set; }
}
Run Code Online (Sandbox Code Playgroud) 我在框架中看到了几个具有此行为的类(监视窗口中的刷新符号和警告).这是由属性控制的吗?如果是这样,我怎样才能在我的库中模拟这个?
编辑:感谢您的信息!为了澄清,我正在开发一个具有必须从单个线程访问数据的属性的框架.不幸的是,当我在调试器中时,由于监视窗口等,我得到一些奇怪的行为.我有使用Debugger Browsable属性的经验; 但是,我希望在主线程访问/设置它们之后显示属性.我已经看到,尤其是在IEnumerables中,如果没有用户输入,调试器将无法进行评估....有没有办法将这些属性标记为需要"隐含评估",或者我可以不吃蛋糕而且也吃它?
.net ×3
c# ×2
architecture ×1
attributes ×1
c++ ×1
frameworks ×1
ide ×1
java ×1
optimization ×1
performance ×1
singleton ×1