c++ - 如何使用执行代码来求解矩阵,同时测量代码的运行时间?

标签 c++ algorithm math matrix strassen

我最好使用 C++ 来执行代码,但我愿意接受任何关于针对这种情况的更好语言的建议。我本质上想使用斯特拉森算法来求解矩阵,并且我想知道如何求解矩阵并测量其运行时间。 # 版本3.6

import numpy as np 

def split(matrix): 
""" 
    Splits a given matrix into quarters. 
    Input: nxn matrix 
    Output: tuple containing 4 n/2 x n/2 matrices corresponding to a, b, c, d 
"""
row, col = matrix.shape 
row2, col2 = row//2, col//2
return matrix[:row2, :col2], matrix[:row2, col2:], matrix[row2:, :col2], 
matrix[row2:, col2:] 

def strassen(x, y): 
""" 
Computes matrix product by divide and conquer approach, recursively. 
Input: nxn matrices x and y 
Output: nxn matrix, product of x and y 
"""

# Base case when size of matrices is 1x1 
if len(x) == 1: 
    return x * y 

# Splitting the matrices into quadrants. This will be done recursively 
# untill the base case is reached. 
a, b, c, d = split(x) 
e, f, g, h = split(y) 

# Computing the 7 products, recursively (p1, p2...p7) 
p1 = strassen(a, f - h) 
p2 = strassen(a + b, h)      
p3 = strassen(c + d, e)        
p4 = strassen(d, g - e)      
p5 = strassen(a + d, e + h)      
p6 = strassen(b - d, g + h) 
p7 = strassen(a - c, e + f) 

# Computing the values of the 4 quadrants of the final matrix c 
c11 = p5 + p4 - p2 + p6 
c12 = p1 + p2        
c21 = p3 + p4            
c22 = p1 + p5 - p3 - p7 

# Combining the 4 quadrants into a single matrix by stacking horizontally and vertically. 
c = np.vstack((np.hstack((c11, c12)), np.hstack((c21, c22)))) 

return c 

我找到了上面的算法代码。

#include <time.h>
int main(void) {
clock_t tStart = clock();
/* Do your stuff here */
printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
return 0;

}

我找到了这个用于测量代码运行时间的代码。不过我发现我可以使用

/usr/bin/time ./MyProgram if I have cygwin installed.

简而言之,我将如何使用我的代码使用 Strassen 算法和其他矩阵求解算法来求解实际矩阵?另外我将如何运行代码?谢谢您的帮助,我是编码新手,这样做是为了测试不同矩阵求解算法在不同场景下的算法效率。

最佳答案

  1. 时间测量

    时间的测量取决于平台,那么什么操作系统呢?在 Windows 上我会使用 Performance Counters 。如果您可以访问 x86 程序集,您也可以使用 RDTSC 指令,但这需要一些知识才能正确使用,例如设置与单个 CPU 的关联性、获取并稳定 CPU 频率等。

    操作系统 time granularity这也是一个问题,因此如果您的测量过程太短,您可能需要对多个测量进行一些过滤才能获得正确的值。

    您可以通过测量该过程的多次重复来避免一些问题,这样时间就会超过 100 毫秒,然后将结果时间除以重复次数。

    此外,在测量 CACHE can be a problem 时重复使用相同的代码/数据太打乱你的结果了。

  2. 运行代码

    你的代码看起来像Python,所以你不能直接在C/C++中使用它,而是需要使用Python解释器以某种方式调用它,例如creating python process带有告诉它打开并运行源代码的参数。然而,在这种情况下,您需要通过扫描其句柄(如果它仍然有效)来等待代码完成...粗略地说,您需要以完成后关闭的方式编写和执行您的Python内容。然而,我担心这会增加很大的开销,因为单独启动/停止 python 进程可能比您测量的矩阵乘法慢得多...

    另一种选择是将其采用 DLL 或 OBJ 形式,然后将其导入到 C/C++ 中(但不确定这对于 Python 代码是否可行)。这样,您只需在 C/C++ 应用程序中调用函数,这样就不会出现问题...

    有关一些灵感,请参阅:

    如果代码不太复杂或者不需要其他库之类的东西,您可以尝试将其移植到 C/C++ 代码并直接使用。

关于c++ - 如何使用执行代码来求解矩阵,同时测量代码的运行时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66149582/

相关文章:

algorithm - Big O 规则 - 问题

javascript - 围绕给定坐标旋转像素

algorithm - 重心坐标下三角点检验的数值稳定性

c++ - 使用 SetUnhandledExceptionFilter() 为访问冲突异常创建小型转储

c++ - 有没有更简单的方法来使用 GDB 转储在 bin 文件中捕获的 C 结构

C# 递归, "Data Structures and Algorithms"。该程序如何在不覆盖自身的情况下打印单独的路线?

math - 计算表示任意基数中的整数所需的长度

c++ - 排序对象和多态性

c++ - __PRETTY_FUNCTION__ 在常量表达式中

python - 在这个算法中使用计数排序有什么好处?