我在.NET应用程序中遇到了一种奇怪的行为,它对一组内存数据执行一些高度并行的处理.
当在多核处理器(IntelCore2 Quad Q6600 2.4GHz)上运行时,它会展示非线性缩放,因为多个线程被启动以处理数据.
当作为单核上的非多线程循环运行时,该过程能够每秒完成大约240万次计算.当作为四个线程运行时,您可以预期吞吐量的四倍 - 在每秒900万次计算的某个地方 - 但是,唉,没有.在实践中,它每秒仅完成约4.1百万......与预期的吞吐量相当短.
此外,无论我使用PLINQ,线程池还是四个显式创建的线程,都会发生这种情况.很奇怪...
使用CPU时间没有其他任何东西在机器上运行,计算中也没有任何锁或其他同步对象......它应该只是在数据中前进.我已经通过在进程运行时查看perfmon数据来确认这一点(尽可能)...并且没有报告的线程争用或垃圾收集活动.
我的理论目前:
以下是代码中应该表现出相同行为的代表性摘录:
var evaluator = new LookupBasedEvaluator();
// find all ten-vertex polygons that are a subset of the set of points
var ssg = new SubsetGenerator<PolygonData>(Points.All, 10);
const int TEST_SIZE = 10000000; // evaluate the first 10 million records
// materialize the data into memory...
var polygons = ssg.AsParallel()
.Take(TEST_SIZE)
.Cast<PolygonData>()
.ToArray();
var sw1 = Stopwatch.StartNew();
// for loop completes in about 4.02 seconds... …Run Code Online (Sandbox Code Playgroud) 现代浏览器保存密码.哪个标准会影响这个决定?
背景:
我有一个注册页面,其中包含以下表格:
<form action="/BlaBla/Account/Register" method="post">
<div>
<fieldset>
<legend>Account Information</legend>
<p>
<label for="username">
Username:</label>
<input id="username" name="username" type="text" value="" />
</p>
<p>
<label for="email">
Email:</label>
<input id="email" name="email" type="text" value="" />
</p>
<p>
<label for="invitationCode">
Invitation Code:</label>
<input id="invitationCode" name="invitationCode" type="text" value="" />
</p>
<p>
<label for="securityQuestion">
Security question:</label>
<input id="securityQuestion" name="securityQuestion" type="text" value="" />
</p>
<p>
<label for="securityAnswer">
Security answer:</label>
<input id="securityAnswer" name="securityAnswer" type="text" value="" />
</p>
<p>
<label for="password">
Password:</label>
<input id="password" name="password" type="password" />
</p>
<p>
<label …Run Code Online (Sandbox Code Playgroud) 大家好,
对不起,这是一个菜鸟问题.我只是不知道如何说出这个过程,所以我不确定谷歌会采取什么措施.我将在下面放一些C#代码来解释我正在尝试做什么.我只是不知道如何在VB中做到这一点.另外,对于将来的参考,如果你能告诉我这个过程被称为什么,那么知道它会很有帮助.在此先感谢您的帮助.
// Here is a simple class
public class FullName
{
public string First { get; set; }
public char MiddleInintial { get; set; }
public string Last { get; set; }
public FullName() { }
}
/* code snipped */
// in code below i set a variable equal to a new FullName
// and set the values in the same line of code
FullName fn = new FullName() { First = "John", MiddleInitial = 'J', Last …Run Code Online (Sandbox Code Playgroud) 在回答这个问题时,我遇到了一个我不理解的情况.OP正在尝试从以下位置加载XML:http://www.google.com/ig/api?weather = 12414&hl =
明显的解决方案是:
string m_strFilePath = "http://www.google.com/ig/api?weather=12414&hl=it";
XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.Load(m_strFilePath); //Load NOT LoadXml
Run Code Online (Sandbox Code Playgroud)
然而,这失败了
XmlException:给定编码中的字符无效.第1行,第499位.
这似乎是在窒息à的Umidità.
OTOH,以下工作正常:
var m_strFilePath = "http://www.google.com/ig/api?weather=12414&hl=it";
string xmlStr;
using(var wc = new WebClient())
{
xmlStr = wc.DownloadString(m_strFilePath);
}
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlStr);
Run Code Online (Sandbox Code Playgroud)
我为此感到困惑.任何人都可以解释为什么前者失败,但后者工作正常吗?
值得注意的是,文档的xml声明省略了编码.
我刚刚学会了互锁类,它应该比简单锁定更快.现在,这一切都很好,但我对实施感到好奇.
据我所知,确保变量操作是以原子方式完成的唯一方法是确保只有一个线程可以随时访问该变量.哪个是锁定的.
我用反射器来获取Interlocked的来源,但似乎它使用外部方法来完成它的所有工作:
[MethodImpl(MethodImplOptions.InternalCall), ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
internal static extern int ExchangeAdd(ref int location1, int value);
Run Code Online (Sandbox Code Playgroud)
我已经运行了一些测试,实际上Interlocked只是锁定对象并增加它的速度的两倍.
他们是怎么做到的?
当在联合类型中使用条件类型时,我遇到了类型推断问题。
可能有一种更短的方法来证明这个问题,但我找不到一个......
在此Playground 链接中查看实际问题。
考虑以下情况Result<T>,联合类型用于指示操作的成功或失败(带有可选的附加值,类型T)。对于成功案例,我使用了条件 type SuccessResult<T>,它解析为OKResultor ValueResult<T>(取决于结果是否还应带有 attach value):
type Result<T = undefined> = SuccessResult<T> | ErrorResult;
interface OKResult {
type: 'OK';
}
interface ValueResult<T> {
type: 'OK';
value: T;
}
interface ErrorResult {
type: 'Error';
error: any;
}
type SuccessResult<T = undefined> = T extends undefined ? OKResult : ValueResult<T>;
function isSuccess<T>(result: Result<T>): result is SuccessResult<T> {
return result.type === 'OK';
}
Run Code Online (Sandbox Code Playgroud)
让我们用一个简单的联合类型来使用它:
type C1 = "A1" …Run Code Online (Sandbox Code Playgroud) generics typescript typescript-generics union-types conditional-types
我使用实现IHttpAsyncHandler的通用处理程序遇到了一些性能问题.最简单的是,处理程序接收GET请求,20秒后在响应中写入"<timeout />"后结束响应.
当使用10000-20000个并发请求锤击.ashx时,在准确的5000个请求之后,503服务器不可用.切换到同步模式并立即结束请求时,问题就消失了.
我已经修改了许多设置,但我唯一能够实现的是降低发生此错误的请求阈值.
这是我玩过的设置的简要说明:
machine.config中:
<configuration>
...
<system.web>
...
<processModel enable="true" requestQueueLimit="10000"/>
...
Run Code Online (Sandbox Code Playgroud)
web.config中:
<configuration>
...
<system.web>
...
<httpRuntime enable="true" appRequestQueueLimit="10000"/>
...
Run Code Online (Sandbox Code Playgroud)
IIS管理器> ApplicationPools>高级设置
Queue Length : 65535
Run Code Online (Sandbox Code Playgroud)
虽然我不能确定,但是如果请求是同步的,那么这些设置似乎工作正常,但是当异步时,在服务器开始告诉我离开之前,我似乎无法超越5000个请求.如果我把事情设置得更低(不能确切地记住上面的设置,但我已经尝试了所有设置),那么503计数会相应地上升,但是当负载严重时我永远不能阻止它超过5000 .
似乎有很多设置分散在无数的地方可能会影响到这一点,但5000似乎相当固定.我在这里看到appRequestQueueLimit不能超过5000,但是找不到关于此的更多信息,并想知道这是否是错误的信息.
IIS中是否存在任何类型的"泛滥控制"设置,可能会将单个主机限制为不超过5000个请求?如何让IIS处理更多5000个并发异步请求?
编辑2:是否有任何计数器或其他指标可能超出限制,我将如何进一步调查?
编辑:这是loadgenerator代码:
using System;
using System.Net;
using System.Threading;
namespace HammerTime
{
class Program
{
private static int counter = 0;
static void Main(string[] args)
{
var limit = 5000;
ServicePointManager.DefaultConnectionLimit=limit;
for (int i = 0; i < limit;++i )
{
StartWebRequest(i.ToString());
}
Console.ReadLine();
}
private …Run Code Online (Sandbox Code Playgroud) 我已经困惑了几天......随意拍下我的任何假设.
我们正在使用带整数键的字典.我假设在这种情况下密钥的值直接用作哈希.这是否意味着(如果密钥分组在一个小范围内)密钥哈希的分布(与密钥本身相同,对吗?)将处于类似的小范围内,因此哈希表的选择不好?
是否更好的是提供一个IEqualityComparer,用素数和模数学做一些聪明的东西来计算更好的分布式哈希?
我正在编写将产生两个线程的代码,然后等待它们使用CyclicBarrier类进行同步.问题是循环障碍没有按预期工作,主线程不等待各个线程完成.以下是我的代码的外观:
class mythread extends Thread{
CyclicBarrier barrier;
public mythread(CyclicBarrier barrier) {
this.barrier = barrier;
}
public void run(){
barrier.await();
}
}
class MainClass{
public void spawnAndWait(){
CyclicBarrier barrier = new CyclicBarrier(2);
mythread thread1 = new mythread(barrier).start();
mythread thread2 = new mythread(barrier).start();
System.out.println("Should wait till both threads finish executing before printing this");
}
}
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么吗?或者有更好的方法来编写这些屏障同步方法吗?请帮忙.
经过大量测量后,我发现了一个我想要优化的Windows服务中的热点.我们正在处理可能有多个连续空格的字符串,我们希望减少到只有一个空格.我们使用静态编译的正则表达式来执行此任务:
private static readonly Regex
regex_select_all_multiple_whitespace_chars =
new Regex(@"\s+",RegexOptions.Compiled);
Run Code Online (Sandbox Code Playgroud)
然后按如下方式使用它:
var cleanString=
regex_select_all_multiple_whitespace_chars.Replace(dirtyString.Trim(), " ");
Run Code Online (Sandbox Code Playgroud)
这条线被调用了数百万次,并且被证明是相当密集的.我试着写一些更好的东西,但我很难过.鉴于正则表达式的处理要求相当适中,肯定会有更快的速度.可以unsafe用指针速度的东西进一步处理?
编辑:
感谢对这个问题的惊人反应......最让人意想不到的!
c# ×6
ashx ×1
asp.net ×1
browser ×1
concurrency ×1
dictionary ×1
forms ×1
generics ×1
hash ×1
iis-7 ×1
interlocked ×1
java ×1
linq ×1
load ×1
locking ×1
optimization ×1
passwords ×1
performance ×1
plinq ×1
regex ×1
string ×1
typescript ×1
union-types ×1
username ×1
vb.net ×1
xmldocument ×1
xmlexception ×1