我正在玩一些javascript性能优化,并发现了一些有趣的东西.这是代码:
function gObject() {
this.obj = [];
this.LIMIT = 100000;
this.doLoopLocal = function () {
var o = [];
for (var i=0;i<this.LIMIT;i+=1) {
o.push(i);
}
return o;
};
this.doLoopObject = function () {
this.obj = [];
for (var i=0;i<this.LIMIT;i+=1) {
this.obj.push(i);
}
};
};
var g = new gObject();
console.time('Using Local array');
g.doLoopLocal();
console.timeEnd('Using Local array');
console.time('Using Object array');
g.doLoopObject();
console.timeEnd('Using Object array');
Run Code Online (Sandbox Code Playgroud)
当我运行它时,日志告诉我使用本地数组比使用对象命名空间中定义的数组慢.差异很大 - 8到10倍!(FF 18.0.1)
Using Local array: 16ms
Using Object array: 2ms
Run Code Online (Sandbox Code Playgroud)
截图:
我总是假设在函数中使用本地定义的对象更快,但是这个实验表明我错了.为什么会这样?
更新:我在本地Firefox控制台中尝试了这个脚本,数字是我首先想到的:使用对象数组使用本地数组优于.因此,真正的原因是Firebug由于某种原因使数字偏斜并显示不正确的结果.要记住的事情.
我现在正在玩竹子.
我的[HttpGet]Web API 2控制器中有一个方法:
[HttpGet]
public LanguageResponse GetLanguages(LanguageRequest request)
{
...
}
Run Code Online (Sandbox Code Playgroud)
我的网址看起来像这样
http://localhost:1234/api/MyController/GetLanguages?Id=1
当尝试调用它时(例如,通过Postman),我收到HTTP错误415:
{"message":"The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource.","exceptionMessage":"No MediaTypeFormatter is available to read an object of type 'LanguagesRequest' from content with media type 'application/octet-stream'.","exceptionType":"System.Net.Http.UnsupportedMediaTypeException","stackTrace":" at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"}
Run Code Online (Sandbox Code Playgroud)
这有点奇怪,因为我正在使用GET.
如果我添加 …
为什么会这样?请遵守以下代码:
static class StringExtension
{
public static string Remove(this string s, char c)
{
return s.Replace(c.ToString(), "");
}
public static string Remove(this string s, char[] a)
{
foreach (char c in a)
{
s = s.Remove((char)c); // <---- ArgumentOutOfRange Exception here
}
return s;
}
}
class Program
{
static void Main(string[] args)
{
char[] a = new char[] { '.', ',' };
string testString = "Clean.this,string.from,periods.and,commas.";
Console.WriteLine(testString.Remove(a));
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我在指示行得到一个ArgumentOutOfRange异常.事实证明,即使我有一个特定的扩展名代码Remove(this,char)和我明确地(虽然,没有理由这样)指定参数的类型,它会忽略我的扩展并尝试调用原始的Remove( int)方法.
我做错了什么或者这是C#中的错误?
PS我用的是VS2010.
我研究JavaScript并想要一些关于生成随机整数的信息.我看它在google和stackoverflow和主要被找到这样的代码(在情况下,我们希望数字1到52):
var randInt=function(){
number=Math.floor(Math.random()*52+1);
};
Run Code Online (Sandbox Code Playgroud)
和
var randNumMin = 1;
var randNumMax = 52;
var randInt = function (){
number = (Math.floor(Math.random() * (randNumMax - randNumMin + 1)) + randNumMin);
};
Run Code Online (Sandbox Code Playgroud)
我读到一些参考Math.random,发现它是从生成的数字0来1.如果Math.random生成1,我们将得到数字5,这意味着我们将得到错误.我同意这是非常罕见的情况,但它是可能的.我略微修改了代码以避免该错误(在我们的例子中生成数字53).在这里,我认为是JavaScript中生成随机数的正确代码.在您的示例中,它只生成整数,但我认为可以修改代码并生成任何类型的数字:
var randInt = function(){
number = Math.floor(Math.random()*52+1);
if (number === 53){
randInt();
}
};
Run Code Online (Sandbox Code Playgroud)
和
var randNumMin = 1;
var randNumMax = 52;
var randInt = function (){
number = …Run Code Online (Sandbox Code Playgroud) 域服务实现应位于DDD项目结构中的什么位置?如果我们有IDomainInterface和DomainInterface实施,应DomainInterface执行驻留在解决方案/项目的基础设施或核心/域的一部分?