快问.我目前正在设计一些数据库查询,以便将相当大但不是大量的数据集提取到内存中,比如大约10k-100k的记录.
到目前为止,我一直在测试将这些结果集加载到scala.collection.immutable.Seq中,并发现它似乎花费了相当长的时间来构建集合.然而,如果我更改为Vector或List,则写入内存只需几分之一秒.
我的问题是,为什么Seq在这种情况下如此缓慢?如果是这样,在什么情况下使用Seq比Vector更合适?
谢谢
我有一个程序,从数据库中读取大约200万行到List.每行是包含地理坐标等信息的位置.
将数据添加到List后,我使用foreach循环并获取坐标以创建kml文件.当行数很大时,循环遇到OutOfMemoryException错误(但是否则完美地工作).
有关如何处理此问题的任何建议,以便程序可以处理非常大的数据集?kml库是SharpKML.
我还是C#的新手,所以请放轻松!
这是循环:
using (SqlConnection conn = new SqlConnection(connstring))
{
conn.Open();
SqlCommand cmd = new SqlCommand(select, conn);
using (cmd)
{
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
double lat = reader.GetDouble(1);
double lon = reader.GetDouble(2);
string country = reader.GetString(3);
string county = reader.GetString(4);
double TIV = reader.GetDouble(5);
double cnpshare = reader.GetDouble(6);
double locshare = reader.GetDouble(7);
//Add results to list
results.Add(new data(lat, lon, country, county, TIV, cnpshare, locshare));
}
reader.Close();
}
conn.Close();
}
int count = results.Count();
Console.WriteLine("number of …Run Code Online (Sandbox Code Playgroud) 我正在尝试构建一个将任何IEnumerable转换为对象[,]的泛型方法.这样做的目的是通过ExcelDNA插入excel,理想情况下需要2d对象数组.
我是新思考的人,需要一些认真的帮助来填补这里的空白.下面发布的代码是我到目前为止所需要的,我需要的是在外部循环中获取DataSource的索引i处的T属性.在内部循环中,然后依次获取每个属性的值并插入到对象[,]中.
任何帮助赞赏.谢谢理查德
public object[,] ConvertListToObject<T>(IEnumerable<T> DataSource)
{
int rows = DataSource.Count();
//Get array of properties of the type T
PropertyInfo[] propertyInfos;
propertyInfos = typeof(T).GetProperties(BindingFlags.Public);
int cols = propertyInfos.Count(); //Cols for array is the number of public properties
//Create object array with rows/cols
object[,] excelarray = new object[rows, cols];
for (int i = 0; i < rows; i++) //Outer loop
{
for(int j = 0; j < cols; j++) //Inner loop
{
object[i,j] = //Need to insert each property val …Run Code Online (Sandbox Code Playgroud) 我试图稍微远离LINQ,这已被证明非常有用整体,但有时也很难阅读.
我曾经使用LINQ来执行连接(完全外连接),但是为了简单起见,我更喜欢使用for/foreach循环.我刚刚将一个LINQ语句(不是PLINQ)转换为嵌套的foreach循环,性能受到了严重影响.过去需要几秒钟的时间现在需要大约一分钟,请参阅下面的代码.
foreach (var p in PortfolioELT)
{
double meanloss;
double expvalue;
double stddevc;
double stddevi;
bool matched = false;
foreach (var a in AccountELT)
{
if (a.eventid == p.eventid)
{ DO SOME MATH HERE <-----
Run Code Online (Sandbox Code Playgroud)
任何一个想法
该程序显然可以满足需要,但速度太慢.
编辑:旧代码已满
public static ConcurrentList<Event> CreateNewELTSUB(IList<Event> AccountELT, IList<Event> PortfolioELT)
{
if (AccountELT == null)
{
return (ConcurrentList<Event>)PortfolioELT;
}
else
{
//Subtract the Account ELT from the Portfolio ELT
var newELT = from p in PortfolioELT
join a in AccountELT
on …Run Code Online (Sandbox Code Playgroud) 我正在使用C#创建的库.我一直在努力将一些代码移植到F#,但必须使用C#lib中的一些底层类型.
一段代码需要计算值列表并将其分配给类中的公共字段/属性.该字段是一个包含两个ICollection的C#类.
我的F#代码工作正常,需要返回F#Seq/List.
我尝试了以下代码片段,每个代码片段都会产生错误.
类中的公共字段,它本身包含两个ICollection对象
this.field.Collection1 = recoveries
Run Code Online (Sandbox Code Playgroud)这给出了错误Expected具有类型ICollection但具有类型Recoveries列表
this.field.Collection1 = new ResizeArray<Recoveries>()
Run Code Online (Sandbox Code Playgroud)
给出错误预期类型ICollection但是ResizeArray
this.field.Collection1 = new System.Collections.Generic.List<Recoveries>()
Run Code Online (Sandbox Code Playgroud)
与上面相同的错误 - 预期的ICollection但类型是List
有任何想法吗?从C#的角度来看,这些操作似乎是有效的,而List/ResizeArray实现了ICollection所以...我很困惑如何分配值.
我可以更改底层C#库的类型,但这可能有其他含义.
谢谢
我有这个基类创建一个新的SQL Server连接并在C#中做一些实用工具方法,我想在F#中继承.目前我无法从F#访问C#类中的受保护字段,尽管这可以在C#中使用.
C#抽象类
public abstract class SQL
{
protected readonly SqlConnectionStringBuilder bld;
protected readonly SqlCommand cmd;
protected readonly SqlConnection conn;
protected readonly string connstring;
protected string userid;
public SQL(string server, string db)
{
bld = new SqlConnectionStringBuilder();
bld.DataSource = server;
bld.InitialCatalog = db;
bld.IntegratedSecurity = true;
connstring = bld.ConnectionString;
conn = new SqlConnection(connstring);
cmd = new SqlCommand();
GetUserID();
//Other utility methods here
}
Run Code Online (Sandbox Code Playgroud)
要继承的F#代码
type Transfer ( server : string ) ( db : string ) =
inherit SQL(server, db) …Run Code Online (Sandbox Code Playgroud) 我正在使用第三方API,而笨拙地利用ref参数来产生输出.就个人而言,我真的很讨厌这个API的设计,但这是我现在可以使用的.由于专有代码,我不得不略微隐藏API的数据类型,但这应该与手头的问题无关.
无论如何,在C#中,我可以成功地将null引用作为ref参数传递,如下所示:
IDataType tl = null;
bool success = api.myFunction(ref tl);
Run Code Online (Sandbox Code Playgroud)
但是在F#中,以下内容不起作用
let mutable tl : IDataType = null //null reference assignment in F#
let success = api.myFunction(&tl) //& means ref in F#
Run Code Online (Sandbox Code Playgroud)
它返回一个空引用异常错误.C#中没有返回此类错误.
有没有人经历过这个?我认为它必须是API本身的一个相对古老的设计中的错误.
**编辑:这应该是关闭的,我相信答案不在于F#代码,而在于API,因为它已经有许多与此类似的已知错误.
通过尝试使用递归重新创建一个简单的二分法来继续我的F#学习,这里我使用MathNet库继承Beta分布.
我收到函数'search'(二进制搜索方法)的错误the value is not a function and cannot be applied.
//Beta class inheriting from MathNet Beta distribution
//Extends the class by implementing an InverseCDF function
type newBeta(alpha:double, beta:double) =
inherit MathNet.Numerics.Distributions.Beta(alpha, beta)
member this.InverseCDF(p: float) =
let rec search (min: float, max: float, acc: uint32) =
let x = (min + max) / 2.0
let error = 0.001
let maxiters : uint32 = 1000u
let cdf = this.CumulativeDistribution(x)
match cdf, (Math.Abs(cdf - p) < error || acc …Run Code Online (Sandbox Code Playgroud) f# ×4
c# ×3
foreach ×2
collections ×1
function ×1
generic-list ×1
generics ×1
icollection ×1
inheritance ×1
protected ×1
reflection ×1
scala ×1
seq ×1
sharpkml ×1