我是否应该在每次想要从存储中获取blob时创建CloudStorageAccount和CloudBlobClient实例?例如,我实现了自定义虚拟路径提供程序以使用blob存储.什么是最好的设计解决方案:创建CloudStorageAccount和CloudBlobClient情况下,一次作为我的自定义虚拟路径提供的私有字段,或使用静态的效用,创造CloudStorageAccount和CloudBlobClient实例(共享)方法,我想从一个blob每次存储?从性能的角度来看它有多贵?
请考虑以下简单示例.
type PaymentInstrument =
| Check of string
| CreditCard of string * DateTime
let printInstrumentName instrument =
match instrument with
| Check number-> printfn "check"
| CreditCard (number, expirationDate) -> printfn "card"
let printRequisites instrument =
match instrument with
| Check number -> printfn "check %s" number
| CreditCard (number, expirationDate) -> printfn "card %s %A" number expirationDate
Run Code Online (Sandbox Code Playgroud)
如您所见,在两个函数中重复相同的模式匹配逻辑.如果我要使用OOP,我会创建接口IPaymentInstrument
,定义两个操作:
PrintInstrumentName
和 PrintRequisites
然后实现类 - 每个支付工具一个.要根据某些外部条件实例化仪器,我会使用(例如)工厂模式(PaymentInstrumentFactory
).
如果我需要添加一个新的支付工具,我只需要添加一个实现IPaymentInstrument
接口和更新工厂实例化逻辑的新类.使用这些类的其他代码保持不变.
但是如果我使用函数方法,我应该更新这种类型的模式匹配存在的每个函数.
如果有很多函数使用PaymentInstrument
类型将是一个问题.
如何使用功能方法消除这个问题?
我开始学习F#,我注意到C#语法的一个主要区别是类型推断比C#使用得多.这通常表现为F#的好处之一.为什么类型推断表现为有益?
想象一下,你有一个类层次结构和使用不同类的代码.强类型允许您快速检测在任何方法中使用的类.使用类型推断它不会那么明显,你必须使用提示来理解,使用哪个类.是否存在任何使用类型推断使F#代码更具可读性的技术?
最近我有一个奇怪的性能问题.我需要比较周期中的时间间隔和大量的迭代.我使用DateTime.TimeOfDay属性来比较这些间隔.但是,我发现这些比较与DateTime比较相比非常慢.因此,我必须创建1年1个月和1天的DateTime,以加快时间间隔比较.我准备了一个小例子来说明我的意思.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DatesBenchmark
{
class Program
{
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
DateTime firstDate = DateTime.Now;
DateTime secondDate = DateTime.Now.AddSeconds(5);
for (int i = 0; i < 2000000; i++)
{
var a = firstDate.TimeOfDay > secondDate.TimeOfDay;
//var a = firstDate > secondDate;
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
Console.ReadKey();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的笔记本电脑上有15毫秒(如果循环中的第一行被评论)与176毫秒(如果循环中的第二行被评论).
我的问题很简短.为什么?