小编zaz*_*azz的帖子

为什么在预转置矩阵上执行矩阵乘法比在非转置矩阵上执行矩阵乘法更快?

考虑 Python 中的以下代码,其中与乘以非转置矩阵相比,乘以预转置矩阵会产生更快的执行时间:

import numpy as np
import time

# Generate random matrix
matrix_size = 1000
matrix = np.random.rand(matrix_size, matrix_size)

# Transpose the matrix
transposed_matrix = np.transpose(matrix)

# Multiply non-transposed matrix
start = time.time()
result1 = np.matmul(matrix, matrix)
end = time.time()
execution_time1 = end - start

# Multiply pre-transposed matrix
start = time.time()
result2 = np.matmul(transposed_matrix, transposed_matrix)
end = time.time()
execution_time2 = end - start

print("Execution time (non-transposed):", execution_time1)
print("Execution time (pre-transposed):", execution_time2)
Run Code Online (Sandbox Code Playgroud)

令人惊讶的是,预转置矩阵的乘法速度更快。人们可能会认为乘法的顺序不会显着影响性能,但似乎存在差异。

为什么与非转置矩阵相比,处理预转置矩阵会导致更快的执行时间?是否有任何根本原因或优化可以解释这种行为?

更新

我已经考虑了有关的评论cache,并且在每个循环上生成新的矩阵:

import …
Run Code Online (Sandbox Code Playgroud)

python transpose numpy matrix

10
推荐指数
1
解决办法
539
查看次数

Python:错误地使用类变量?

我有以下课程:

class A(object):
    x = 1
class B(A):
    pass
class C(A):
    pass
Run Code Online (Sandbox Code Playgroud)

当我打印x每个类的值时,我得到:

>>>A.x, B.x, C.x
(1,1,1)
Run Code Online (Sandbox Code Playgroud)

然后我分配2B.x

B.x = 2
A.x, B.x, C.x
>>>(1,2,1)
Run Code Online (Sandbox Code Playgroud)

一切正常,但是当我分配3A.x我时:

A.x=3
A.x, B.x, C.x
>>>(3,2,3)
Run Code Online (Sandbox Code Playgroud)

我以为它会回来(3,2,1)

python

7
推荐指数
1
解决办法
119
查看次数

有效地对大型图像数据矩阵求和

如何最有效地对大文件求和(100, 1024, 1024) .npz?有没有更好的文件格式来存储给定维度的Python数据?

我目前用它来对矩阵求和:

summed_matrix = np.sum([np.load(file) for file in files_list])
Run Code Online (Sandbox Code Playgroud)

这超出了我的可用内存。

python numpy

3
推荐指数
2
解决办法
74
查看次数

CSS Grid - 底部的空白以及如何删除它

我正在尝试掌握 CSS Grid,这是从以前使用我习惯的 Bootstrap 的过渡。

我创建了一个简单的布局(4 行和 6 列),但是底部有一个不需要的空白,导致可见的滚动。

有没有办法在不为 .container 元素设置确切高度的情况下解决这个问题?当我将高度设置为 .container (height: 500px) 时,问题就消失了。这是绕过它的方法吗?我不想使用可能在较小或较大视口上导致问题的快速修复。

.grid{
  display: grid;
  position: relative;
  margin: auto;
  grid-template-areas:
    "nav nav nav nav nav nav"
    "logo logo logo logo logo logo"
    "main main main main main side"
    "footer footer footer footer footer footer";

  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: 50px 50px 1fr 1fr;
}
.nav{
  grid-area: nav;
  background-color:green;
}
.logo{
  grid-area: logo;
  background-color:salmon;
}
.main{
  grid-area: main;
  background-color:cadetblue;
  min-height: 800px;
  height: auto;
}
.side{
  grid-area: side;
  background-color:lightpink; …
Run Code Online (Sandbox Code Playgroud)

html css scrollbar css-grid

1
推荐指数
2
解决办法
1万
查看次数

允许一个进程在给定时间内停止另一个进程 c

我想知道是否有办法让父进程在给定时间内停止他的孩子,signal
例如使用:

pid_t pid = fork();

if(pid==0){
  while(1){

//some code here

  }
}else{

// some code to make child process stop for x seconds

}
Run Code Online (Sandbox Code Playgroud)

c linux signals

-2
推荐指数
1
解决办法
149
查看次数

标签 统计

python ×3

numpy ×2

c ×1

css ×1

css-grid ×1

html ×1

linux ×1

matrix ×1

scrollbar ×1

signals ×1

transpose ×1