我几乎完成了一个处理一些非常大的整数的算法(大约2的数量增加到100,000,000的功率).这需要在16核心服务器上使用几个小时的高度并行代码,并且内存足够,因为算法不是内存密集型的.我使用.NET 4中的BigInteger类.
算法的细节并不重要,但对于上下文,以下是对这些整数执行的操作的非常详尽的列表以及算法的一些显着特征:
我已经尽可能地优化了代码,现在分析只显示了两个瓶颈:
考虑到内存访问和日志操作,我开始考虑GPU以及是否可以有效地卸载一些工作.我对GPU知之甚少,只是它们针对浮点运算进行了优化.
我的问题是,使用像GPU .NET这样的库,如何在GPU上处理如此大的数字?我可以以某种方式利用浮点优化来计算如此大数的Log吗?
寻找形成战略的起点.
我正在创建一个接受T的通用Windows窗体,并使用自定义属性的反射在运行时创建标签和输入控件.
例:
class GenericForm<T>: Form where T : ICloneable<T>
{
}
Run Code Online (Sandbox Code Playgroud)
这是表单代码的上一个问题的链接:SO Question.
此表单可以接受以下实体类作为示例:
class Vehicle: ICloneable<Vehicle>
{
public int Id { get; set; }
public int Name { get; set; }
public int Description { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
您可以想象,表单背后的魔力将使用反射来确定数据类型,验证标准,要使用的首选控件类型等.
而不是重新发明轮子,我认为如果有人知道这样的框架,那么就值得询问.不用说,我正在寻找一些简单而不是庞大的框架.
我的应用程序加载位于其执行路径中的所有库程序集,并对包含的类执行预先知道的方法.
我现在需要对引用我的应用程序集的程序集执行相同的操作.这是可能的吗?我应该注意哪些负面影响?
大师集会:
public abstract class TaskBase
{
public abstract void DoWork();
}
LoadAssemblyFromFile("Assembly0001.dll");
Assembly0001.Task1.DoWork();
Run Code Online (Sandbox Code Playgroud)
儿童大会:
public sealed class Task1: MasterAssembly.TaskBase
{
public override void DoWork { /* whatever */ }
}
Run Code Online (Sandbox Code Playgroud) 我的应用程序是WinForms .NET 4(C#),其中一个表单按下按钮后会自动关闭.
我也试图检查stray this.Close/this.Dispose调用但没有找到.
这是代码:
private void ButtonTestConnection_Click (object sender, System.EventArgs e)
{
this.Enabled = false;
this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
this.ProgressBar.Minimum = 0;
this.ProgressBar.Maximum = 500;
this.ProgressBar.Value = 0;
this.ProgressBar.Visible = true;
this.ButtonTestConnection.Visible = false;
try
{
while (this.ProgressBar.Value < this.ProgressBar.Maximum)
{
// Some proxy code.
this.ProgressBar.Value++;
}
}
catch
{
}
this.ProgressBar.Visible = false;
this.ButtonTestConnection.Visible = true;
this.ProgressBar.Invalidate();
System.Windows.Forms.Application.DoEvents();
System.Threading.Thread.Sleep(10);
this.Cursor = System.Windows.Forms.Cursors.Default;
this.Enabled = true;
System.Windows.Forms.MessageBox.Show(result.ToString());
}
Run Code Online (Sandbox Code Playgroud) 我想计算一条线的斜率.
public sealed class Point
{
public System.Numerics.BigInteger x = 0;
public System.Numerics.BigInteger y = 0;
public double CalculateSlope (Point point)
{
return ((point.Y - this.Y) / (point.X - this.X));
}
}
Run Code Online (Sandbox Code Playgroud)
我知道BigInteger有一个DivRem函数可以返回除法结果加上余数,但我不知道如何应用它来得到一个double.我正在处理的数字远远超出了Int64.MaxValue的范围,因此剩余部分本身可能超出了传统划分的范围.
编辑:不确定它是否有帮助,但我只处理正整数(> = 1).
重要提示:我只需要几个小数点的精度(5应该足够我的目的).
我遇到了一个适用于结构的扩展方法,(SomeStruct)并返回该值是否等于default(SomeStruct)(当调用无参数构造函数时).
public static bool IsDefault<T> (this T value)
where T : struct
{
return (!EqualityComparer<T>.Default.Equals(value, default(T)));
}
Run Code Online (Sandbox Code Playgroud)
这让我想知道结构是否被装箱.这纯粹是出于好奇,因为根据上下文有拳击/传递值的优点/缺点.
假设:
==/!=.object.Equals(object o).(object/T)所以我认为它也将避免拳击.但是,目标结构需要实现IEquatable<T>接口,使得辅助扩展方法不是很有用.变化:
public static bool IsDefault<T> (this T value)
where T : struct
{
// Illegal since there is no way to know whether T implements the ==/!= operators.
return (value == default(T));
}
public static bool IsDefault<T> (this T value)
where T : struct …Run Code Online (Sandbox Code Playgroud) 我已经实现了一个简单函数的普通和并行版本,它从32bppArgb位图计算直方图.正常版本在1920x1080图像上大约需要0.03秒,而并行版本需要0.07秒.
线程开销真的很重吗?除了Parallel之外还有其他一些构造吗?这可以加快这个过程吗?因为我正在使用30fps视频,所以我需要加快速度.
这是简化的代码:
public sealed class Histogram
{
public int MaxA = 0;
public int MaxR = 0;
public int MaxG = 0;
public int MaxB = 0;
public int MaxT = 0;
public int [] A = null;
public int [] R = null;
public int [] G = null;
public int [] B = null;
public Histogram ()
{
this.A = new int [256];
this.R = new int [256];
this.G = new int [256];
this.B = new int …Run Code Online (Sandbox Code Playgroud) 此问题与如何重新启动应用程序无关.我已经通过使用Mutex和辅助启动器应用程序实现了这一目标.在使用Application.Restart遇到一些问题后,我不得不求助于此.
无论如何,我不熟悉IL,我想知道是否有人可以解释Application.Restart的工作原理.它是对运行时的调用,但运行时究竟做了什么?它如何关闭现有实例以及它如何知道何时启动新实例?
为什么这个声明+赋值会导致错误:
// Use of unassigned local variable 'handler'.
SessionEndingEventHandler handler = (sender, e) => { isShuttingDown = true; SystemEvents.SessionEnding -= handler; };
Run Code Online (Sandbox Code Playgroud)
虽然这不是:
SessionEndingEventHandler handler = null;
handler = (sender, e) => { isShuttingDown = true; SystemEvents.SessionEnding -= handler; };
Run Code Online (Sandbox Code Playgroud)
直观地说,第一个语句应该导致错误但不能立即清楚为什么第二个语句不是.
此外,SystemEvents.SessionEnding在调用之后,如何判断事件是否实际上已取消订阅handler(null, null)?该GetInvocationList只适用于委托.
SystemEvents.SessionEnding += handler;
handler(null, null);
Run Code Online (Sandbox Code Playgroud) 我有一本字典如下:
public enum Role { Role1, Role2, Role3, }
public enum Action { Action1, Action2, Action3, }
var dictionary = new Dictionary<Role, List<Action>>();
dictionary.Add(RoleType.Role1, new Action [] { Action.Action1, Action.Action2 }.ToList());
Run Code Online (Sandbox Code Playgroud)
现在我希望能够构造一个只读字典,其值类型也是只读的,如下所示:
var readOnlyDictionary = new ReadOnlyDictionary<Role, ReadOnlyCollection<Action>>(dictionary);
Run Code Online (Sandbox Code Playgroud)
由于TValue类型的不同,最后一行显然会导致编译时错误.
使用a List<TValue>也是必要的,因为原始字典是以编程方式从外部源填充的.
有没有简单的方法来执行此转换?
c# ×10
.net ×8
winforms ×2
assemblies ×1
biginteger ×1
boxing ×1
delegates ×1
dictionary ×1
division ×1
equality ×1
generics ×1
gpu ×1
histogram ×1
performance ×1
physics ×1
reference ×1
reflection ×1
runtime ×1
value-type ×1
variables ×1