- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一些不同的模型使用不同的算法执行相同的任务。我想通过测量 CPU 周期来比较这些模型的性能(我尝试使用 System.Diagnostics.StopWatch
来计算 Ticks,但结果不够准确)。
我找到了一个 class,它使用 P/Invoke
测量 CPU 周期,如下所示:
IModel model;
CodeTimer.Time(true, model.ToString(), totalTime, model.TimeStep);
上述方法迭代N
次方法model.TimeStep
。我注意到 CodeTimer.Time
的结果差异很大(至少相差一个数量级,从 58 KCycles
到 890 KCycles
)。因此,如果我错了,请纠正我,但是由于 CodeTimer
类标记了进程前后的周期,它还可以计算在执行期间发生的任何其他进程(甚至操作系统进程)使用的周期model.TimeStep
方法(我想测量性能的方法)。
所以两个问题:
然后我想到了在不同的 System.Threading.Thread
中迭代 model.TimeStep()
并使用 QueryProcessCycleTime
测量给定线程的周期。但是,由于 QueryProcessCycleTime
接收到一个 System.Threading.WaitHandle
(或一个 IntPtr
)作为输入,我不知道如何告诉它我想测量我刚刚创建的特定 Thread
周期的方法。换句话说,我不知道如何将我创建的用于迭代模型的 thread
、WaitHandle
和 QueryProcessCycleTime
放在一起,尽管我已经阅读了一些关于使用 WaitHandle
的 examples(我只是不能将所有内容混合在一起)。
应该是这样的:
class ModelSimulator
{
public Model1 model1 { get; private set; } // implements IModel
public Model2 model2 { get; private set; } // implements IModel
/* other methods */
public void RunModel(object obj)
{
IModel model = (IModel)obj;
Int32 t = model.maxTime;
while (t-- > 0)
model.TimeStep();
}
}
在 main
方法中,我做:
[DllImport("Kernel32", ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern Boolean QueryProcessCycleTime(SafeWaitHandle processHandle, out UInt64 CycleTime);
static void Main(string[] args)
{
ModelSimulator ms = new ModelSimulator();
Thread thread = new Thread(ms.RunModel);
thread.Start(ms.model1);
// here I start the thread, but how can I use QueryProcessCycleTime to measure
// the thread execution
}
PS:我的不是 this question 的副本,因为我需要一些更具体的答案,但那里没有...我的问题可以作为该问题的补充。
最佳答案
正如@ShlomiBorovitz 所建议的,可以使用 GetCurrentThread()
为了识别Thread
这是运行的过程。可能的解决方案是在 CodeTimer.cs
中进行非常简单的修改.可以在此答案的末尾找到所需的修改。我添加了一个名为 PerformanceStatus
的类在 CodeTimer.Time()
的结果中也包含级别 0、1 和 2 的垃圾收集量用于衡量绩效的方法。这样一来,也可以知道垃圾收集器在这个过程中有没有收集到什么东西。
然后你只需要像这样使用它:
ModelSimulator nm = new ModelSimulator();
CodeTimer c = new CodeTimer(nm.RunModel, model); // model implements IModel
UInt64 time = c.Time().CPUCycles;
Console.WriteLine("{0:0.000e+000} KCy", (Double)time / 1000.0D);
类ModelSimulator
在问题中给出,带有方法ModelSimulator.RunModel(IModel model)
它接收类型为 IModel
的对象.
这是 CodeTimer.cs
需要衡量性能
public sealed class CycleTime
{
private Boolean m_trackingThreadTime;
private SafeWaitHandle m_handle;
private UInt64 m_startCycleTime;
private CycleTime(Boolean trackingThreadTime, SafeWaitHandle handle)
{
m_trackingThreadTime = trackingThreadTime;
m_handle = handle;
m_startCycleTime = m_trackingThreadTime ? Thread() : Process(m_handle);
}
[CLSCompliant(false)]
public UInt64 Elapsed()
{
UInt64 now = m_trackingThreadTime ? Thread(/*m_handle*/) : Process(m_handle);
return now - m_startCycleTime;
}
public static CycleTime StartThread(SafeWaitHandle threadHandle)
{
return new CycleTime(true, threadHandle);
}
public static CycleTime StartProcess(SafeWaitHandle processHandle)
{
return new CycleTime(false, processHandle);
}
public static UInt64 Thread(IntPtr threadHandle)
{
UInt64 cycleTime;
if (!QueryThreadCycleTime(threadHandle, out cycleTime))
throw new Win32Exception();
return cycleTime;
}
/// <summary>
/// Retrieves the cycle time for the specified thread.
/// </summary>
/// <param name="threadHandle">Identifies the thread whose cycle time you'd like to obtain.</param>
/// <returns>The thread's cycle time.</returns>
[CLSCompliant(false)]
public static UInt64 Thread(SafeWaitHandle threadHandle)
{
UInt64 cycleTime;
if (!QueryThreadCycleTime(threadHandle, out cycleTime))
throw new Win32Exception();
return cycleTime;
}
[CLSCompliant(false)]
public static UInt64 Thread()
{
UInt64 cycleTime;
if (!QueryThreadCycleTime((IntPtr)(-2), out cycleTime))
throw new Win32Exception();
return cycleTime;
}
/// <summary>
/// Retrieves the sum of the cycle time of all threads of the specified process.
/// </summary>
/// <param name="processHandle">Identifies the process whose threads' cycles times you'd like to obtain.</param>
/// <returns>The process' cycle time.</returns>
[CLSCompliant(false)]
public static UInt64 Process(SafeWaitHandle processHandle)
{
UInt64 cycleTime;
if (!QueryProcessCycleTime(processHandle, out cycleTime))
throw new Win32Exception();
return cycleTime;
}
/// <summary>
/// Retrieves the cycle time for the idle thread of each processor in the system.
/// </summary>
/// <returns>The number of CPU clock cycles used by each idle thread.</returns>
[CLSCompliant(false)]
public static UInt64[] IdleProcessors()
{
Int32 byteCount = Environment.ProcessorCount;
UInt64[] cycleTimes = new UInt64[byteCount];
byteCount *= 8; // Size of UInt64
if (!QueryIdleProcessorCycleTime(ref byteCount, cycleTimes))
throw new Win32Exception();
return cycleTimes;
}
[DllImport("Kernel32", ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern Boolean QueryThreadCycleTime(IntPtr threadHandle, out UInt64 CycleTime);
[DllImport("Kernel32", ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern Boolean QueryThreadCycleTime(SafeWaitHandle threadHandle, out UInt64 CycleTime);
[DllImport("Kernel32", ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern Boolean QueryProcessCycleTime(SafeWaitHandle processHandle, out UInt64 CycleTime);
[DllImport("Kernel32", ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern Boolean QueryIdleProcessorCycleTime(ref Int32 byteCount, UInt64[] CycleTimes);
[DllImport("Kernel32", ExactSpelling = true, SetLastError = true)]
public static extern IntPtr GetCurrentThread();
}
public sealed class CodeTimer //: IDisposable
{
private Int32 m_collectionCount0;
private Int32 m_collectionCount1;
private Int32 m_collectionCount2;
private Thread m_thread;
private IModel m_model;
private Action<IModel> m_performanceMethod;
private UInt64 outThreadCycles;
public CodeTimer(Action<IModel> perfMethod, IModel model)
{
PrepareForOperation();
m_performanceMethod = perfMethod;
m_model = model;
m_thread = new Thread(PerformanceTest);
}
private void PerformanceTest()
{
PrepareForOperation();
IntPtr p = CycleTime.GetCurrentThread();
UInt64 t = CycleTime.Thread(p);
m_performanceMethod(m_model);
outThreadCycles = CycleTime.Thread(p) - t;
}
public PerformanceStatus Time()
{
m_thread.Start();
m_thread.Join();
return new PerformanceStatus(GC.CollectionCount(0) - m_collectionCount0, GC.CollectionCount(1) - m_collectionCount1, GC.CollectionCount(2) - m_collectionCount2, outThreadCycles);
}
private void PrepareForOperation()
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
m_collectionCount0 = GC.CollectionCount(0);
m_collectionCount1 = GC.CollectionCount(1);
m_collectionCount2 = GC.CollectionCount(2);
}
}
public class PerformanceStatus
{
public Int32 GCCount1;
public Int32 GCCount2;
public Int32 GCCount3;
public UInt64 CPUCycles;
public PerformanceStatus(Int32 gc1, Int32 gc2, Int32 gc3, UInt64 cpuCycles)
{
this.GCCount1 = gc1;
this.GCCount2 = gc2;
this.GCCount3 = gc3;
this.CPUCycles = cpuCycles;
}
}
关于c# - 如何测量 C# 中给定进程的 CPU 周期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24984922/
在下面的代码中,我得到一个 uninitialized value警告,但仅限于第二个 given/when例子。为什么是这样? #!/usr/bin/env perl use warnings; u
整个“开关”功能是否已成为实验性的?在没有 Perl 的 future 版本破坏我的代码的情况下,我可以依赖其中的某些部分吗?一般来说,将稳定功能更改为实验性的政策是什么? 背景use feature
有没有办法在一个条件语句中写出如下语句? a和b不能同时等于5。 (a可以是5,b可以是5,但是a AND b不能是5) 最佳答案 正如克里斯指出的那样,您要查找的是逻辑异或,相当于逻辑不等于 !=:
我正在寻找一种算法来找到给定 n 条线段的所有交点。以下是来自 http://jeffe.cs.illinois.edu/teaching/373/notes/x06-sweepline.pdf 的伪
数组中有 N 个元素。我可以选择第一项最多 N 次,第二项最多选择 N-1 次,依此类推。 我有 K 个 token 要使用并且需要使用它们以便我可以拥有最大数量的项目。 arr = [3, 4, 8
我正在尝试修复法语文本中的语法性别,想知道是否有办法从某个词条中获取所有单词的列表,以及是否可以在此类列表中进行查找? 最佳答案 尝试: import spacy lemma_lookup = spa
我正在为 Win32 编写一个简单的自动化测试应用程序。它作为一个单独的进程运行,并通过 Windows API 访问目标应用程序。我可以阅读窗口层次结构,查找标签和文本框,并通过发送/发布消息等来单
在 nodeJs 中使用 Sequelize 时,我从 Sequelize 收到此错误,如下所示: { [SequelizeUniqueConstraintError: Validation erro
本文https://arxiv.org/pdf/1703.10757.pdf使用回归激活映射 (RAM) - 而不是类激活映射 (CAM) 来解决问题。有几篇文章描述了如何实现 CAM。但是我找不到
我正在研究 Mach 动态链接器 dyld。这个问题适用于所有 Apple 平台,但很高兴得到特定于平台的答案;我正在使用 ObjC,但如果对你有用的话,我也很乐意翻译 Swift。 The rele
我有一个包含数千个 Instagram 用户 ID 的列表。我如何获得他们的 Instagram 用户名/句柄? 最佳答案 你必须使用这个 Instagram API: https://api.ins
我在下面的代码: def main(args: Array[String]) { val sparkConf = new SparkConf().setAppName("Spark-Hbase").s
我有一个表格,其中包含从 1 到 10 的数字。(从 D2 到 M2) 假设A1中有03/09/2019 并且在B1中有06/09/2019 并且在C1中有Hello 在A 列中,我有多个系列的单词,
我想在给定服务对应的 URI 的情况下检索服务的注释(特别是 @RolesAllowed )。这是一个例子: 服务: @GET @Path("/example") @RolesAllowed({ "B
我看到 OraclePreparedStatementexecuteQuery() 表现出序列化。也就是说,我想使用相同的连接对 Oracle 数据库同时运行两个查询。然而,OraclePrepare
import java.util.Scanner; public class GeometricSumFromK { public static int geometricSum(int k,
我创建了一个抽象基类Page,它说明了如何构建动态网页。我正在尝试想出一种基于作为 HttpServletRequest 传入的 GET 请求生成 Page 的好方法。例如... public cla
我的字符串是一条短信,采用以下两种格式之一: 潜在客户短信: 您已收到 1 条线索 标题:我的领导 潜在客户 ID:12345-2365 警报设置 ID:890 短信回复: 您已收到 1 条回复 标题
我在 python 中有以下代码: class CreateMap: def changeme(listOne, lisrTwo, listThree, listFour, listfive):
这是在 Hibernate 上运行的 JPA2。 我想检索相同实体类型的多个实例,给定它们的 ID。其中许多已经在持久性上下文和/或二级缓存中。 我尝试了几种方法,但似乎都有其缺点: 当我使用 ent
我是一名优秀的程序员,十分优秀!