经常这样我发现自己对小块代码进行基准测试,看看哪个实现最快.
我经常看到基准测试代码没有考虑到jitting或垃圾收集器的评论.
我有以下简单的基准测试功能,我已经慢慢演变了:
static void Profile(string description, int iterations, Action func) {
// warm up
func();
// clean up
GC.Collect();
var watch = new Stopwatch();
watch.Start();
for (int i = 0; i < iterations; i++) {
func();
}
watch.Stop();
Console.Write(description);
Console.WriteLine(" Time Elapsed {0} ms", watch.ElapsedMilliseconds);
}
Run Code Online (Sandbox Code Playgroud)
用法:
Profile("a descriptions", how_many_iterations_to_run, () =>
{
// ... code being profiled
});
Run Code Online (Sandbox Code Playgroud)
这个实现是否有任何缺陷?是否足以表明实现X比Z迭代实现Y更快?您能想出任何可以改善这种情况的方法吗?
编辑 很明显,基于时间的方法(与迭代相反)是首选,是否有人有任何实施时间检查不会影响性能?
我目前正在使用http://paperjs.org来创建HTML5画布绘图应用程序.我想让用户将图片上传到画布.我知道我需要进行登录和注册,但有更简单的方法吗?我看过HTML5拖放上传.
我正在尝试在success.phtml页面上获取Magento中的订单增量ID,以便我可以将其用于联盟会员跟踪.
我正在使用以下代码,但它在第二行给出错误;
$order = Mage::getSingleton('sales/order')->getLastOrderId();
$lastOrderId = $order->getIncrementId();
Run Code Online (Sandbox Code Playgroud)
错误如下:
致命错误:在第34行的非对象上调用成员函数getIncrementId():$ LastOrderId = $ order-> getIncrementId();
我想知道是否有人对如何获得订单增量ID有任何想法?这是管理员中看到的参考号,通常类似于:#1000123
我是否需要启用交互式桌面才能使其正常工作以及启动EXE或cmd窗口的正确代码是什么?即使我启用它与桌面交互,我仍然无法启动该服务.
我将使用聊天引擎,因此它更容易作为Windows服务进行管理.
我的代码有什么问题?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using System.Diagnostics;
using System.ComponentModel;
using System.Threading;
namespace MyNewService
{
class Program : ServiceBase
{
static void Main(string[] args)
{
}
public Program()
{
this.ServiceName = "Chatter";
}
protected override void OnStart(string[] args)
{
base.OnStart(args);
//TODO: place your start code here
ThreadStart starter = new ThreadStart(bw_DoWork);
Thread t = new Thread(starter);
t.Start();
}
private void bw_DoWork()
{
Process p = new Process();
p.StartInfo = new ProcessStartInfo(@"C:\Windows\system32\cmd.exe");
p.Start();
p.WaitForExit(); …Run Code Online (Sandbox Code Playgroud) 为什么我在以下代码中收到此错误?
void Main()
{
int? a = 1;
int? b = AddOne(1);
a.Dump();
}
static Nullable<int> AddOne(Nullable<int> nullable)
{
return ApplyFunction<int, int>(nullable, (int x) => x + 1);
}
static Nullable<T> ApplyFunction<T, TResult>(Nullable<T> nullable, Func<T, TResult> function)
{
if (nullable.HasValue)
{
T unwrapped = nullable.Value;
TResult result = function(unwrapped);
return new Nullable<TResult>(result);
}
else
{
return new Nullable<T>();
}
}
Run Code Online (Sandbox Code Playgroud) 这是一个面试问题:
将第一个重复元素从整数数组中返回的最佳方法是什么?
例:
给定一个数组[12, 46, 244, 0, 12, 83, 48, 98, 233, 83, 26, 91, 119, 148, 98].
这种情况下的返回值是12.
如何才能做到这一点?
我正在尝试SqlBulkCopy使用SQL Server管理导入导出向导创建的flatfiles中的类进行批量插入.这些文件以逗号分隔.
文件中的一行可能如下所示:
{DCAD82A9-32EC-4351-BEDC-2F8291B40AB3} ,, {ca91e768-072d-4e30-aaf1-bfe32c24008f},900001:1792756,900001:1792757,basladdning,2011-04-29 02:54:15.380000000,basladdning,2011- 04-29 02:54:15.380000000,{20A3C50E-8029-41DE-86F1-DDCDB9A78BA5}
我得到的错误是:
System.InvalidOperationException未处理
Message =数据源中String类型的给定值无法转换为指定目标列的uniqueidentifier类型.
所以问题是从字符串转换为GUID.
我尝试过以下方法:
有什么建议?在''是的平面文件中,是一个也有Guid作为数据类型的列.该列为NULL,向导将其导出为''.这可能是问题吗?
代码:
using (var file = new StreamReader(filePath))
using (var csv = new CsvReader(file, false))
using (var bcp = new SqlBulkCopy(CreateConnectionString()))
{
bcp.DestinationTableName = "table";
bcp.WriteToServer(csv);
}
Run Code Online (Sandbox Code Playgroud)
表定义:
CREATE TABLE [beata].[T_FeatureInstance](
[FeatureInstanceId] [uniqueidentifier] NOT NULL,
[FeatureInstanceParentId] [uniqueidentifier] NULL,
[FeatureTypeAliasId] [uniqueidentifier] NOT NULL,
[Uuid] [varchar](1000) NOT NULL,
[VersionId] [varchar](50) NOT NULL,
[SkapadAv] [varchar](255) NULL,
[Skapad] [datetime] NOT NULL,
[UppdateradAv] [varchar](255) NOT …Run Code Online (Sandbox Code Playgroud) 在SQL Server 2012中我希望concat5列成1但在查询中它可以工作但是当我放入视图时它会给我一个错误
Msg 174,Level 15,State 1,Line 3
CONCAT功能需要2个参数.
有什么问题所以我可以解决它,因为它concat是一个concatenate超过1列的好函数,因为如果它为null,它们会使它为空.
码:
SELECT
'Aan ' + A.Name AS 'Naam',
{ fn CONCAT('T.a.v. ', C.Salutation + ' ', C.FirstName + ' ', C.MiddleName + ' ', C.LastName) } AS 'T.a.v.',
ISNULL(ISNULL(A.Address1_Line2, A.Address1_Line1),
C.Address1_Line2) AS 'Adres',
ISNULL(A.Address1_PostalCode + ' ' + A.Address1_City, A.Address2_PostalCode + ' ' + A.Address2_City) AS 'Woonplaats',
'heer' + ' ' + ISNULL(C.MiddleName + ' ', N'') + ISNULL(C.LastName, N'') AS 'Aanhef'
FROM
dbo.Account …Run Code Online (Sandbox Code Playgroud) c# ×5
php ×2
sql-server ×2
.net ×1
arrays ×1
cmd ×1
concat ×1
email ×1
generics ×1
html5 ×1
java ×1
javascript ×1
magento ×1
nullable ×1
performance ×1
profiling ×1
sqlbulkcopy ×1
windows ×1