加快C代码 [英] speeding up C code
问题描述
我有C代码,它计算矩阵的行和,用行和除以每行的每个元素,然后计算列的总和
结果矩阵。有没有办法可以加速C中的代码:
/ *这是代码* /
//表是wij
int i,j;
for(i = 0; i< N; ++ i)
{
for(j = 0; j< N; ++ j)
{
sum_over_j_wij [i] + = wij [i,j] ;
}
for(j = 0; j< N; ++ j)
{
sum_over_i [j] + = wij [i,j] / sum_over_j_wij [i]];
}
}
I have C code which computes the row sums of a matrix, divide each
element of each row with the row sum and then compute the column sum of
the resulting matrix. Is there a way I can speed up the code in C:
/* Here is the code */
// Table is "wij"
int i, j;
for(i = 0; i < N; ++i)
{
for(j = 0; j < N; ++j)
{
sum_over_j_wij[i] += wij[i,j];
}
for(j = 0; j < N; ++j)
{
sum_over_i[j] += wij[i,j]/sum_over_j_wij[i)];
}
}
推荐答案
dv*****@hotmail.com 写道:
我有C代码,它计算矩阵的行和,用行和除去每行的每个元素,然后计算得到的矩阵的列和。有没有办法可以加速C中的代码:
/ *这是代码* /
//表格是wij
int i,j;
for(i = 0; i< N; ++ i)
{
for(j = 0; j< N; ++ j)> {
sum_over_j_wij [i] + = wij [i,j];
来自win [i,j]你可能的意思是赢[i] [j],不是吗?
}
for(j = 0; j< N; ++ j)
{
sum_over_i [j] + = wij [i,j] / sum_over_j_wij [i]];
}
}
I have C code which computes the row sums of a matrix, divide each
element of each row with the row sum and then compute the column sum of
the resulting matrix. Is there a way I can speed up the code in C:
/* Here is the code */
// Table is "wij"
int i, j;
for(i = 0; i < N; ++i)
{
for(j = 0; j < N; ++j)
{
sum_over_j_wij[i] += wij[i,j];
By "win[i,j]" you probably mean "win[i][j]", don''t you?
}
for(j = 0; j < N; ++j)
{
sum_over_i[j] += wij[i,j]/sum_over_j_wij[i)];
}
}
有一些循环展开技术可能有所帮助。你也可以通过使用指针并使用内置++运算符或+ =运算符使用
推进那些指针来避免索引。
下次,请不要在邮件中输入您的代码,而是使用复制并粘贴代码。所有现代Otes提供的功能
和应用程序。
V
There are some loop unrolling techniques that might help. You can also
avoid indexing all the time by using pointers and advancing those using
the built-in ++ operator or += operator.
Next time, please refrain from typing your code into the message and
instead use the "copy-and-paste" capability offered by all modern Otes
and applications.
V
dvumani写道:
dvumani wrote:
我有C代码,用于计算矩阵的行和,用行和除以每行的每个
元素,然后计算结果的列和矩阵。有没有办法可以加速C中的代码:
sum_over_j_wij [i] + = wij [i,j];
I have C code which computes the row sums of a matrix, divide each
element of each row with the row sum and then compute the column sum of
the resulting matrix. Is there a way I can speed up the code in C: sum_over_j_wij[i] += wij[i,j];
是wij [i ,j]做你认为它做的事情?
你有时间测试这段代码,看看它是否很慢?
您是否对整个应用程序进行了时间测试,看看这个
代码的速度是否相关?
完成所有研究后,切换到C ++并查看up表达
metatemplates。你可能会找到几个矩阵的例子。
-
Phlip
http://www.c2.com/cgi/wiki?ZeekLand
Is wij[i,j] doing what you think it does?
Have you time-tested this code to see if it''s slow?
Have you time-tested your entire application, to see if the speed of this
code is relevant?
After all that research, switch to C++ and look up "expression
metatemplates". You will probably find several examples with matrices.
--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand
< br>
dv*****@hotmail.com 写道:
I有C代码计算矩阵的行和,用行和除去每行的每个元素,然后计算得到的矩阵的列和。有没有办法可以加速C中的代码:
/ *这是代码* /
//表格是wij
int i,j;
for(i = 0; i< N; ++ i)
{
for(j = 0; j< N; ++ j)> {
sum_over_j_wij [i] + = wij [i,j];
}
for(j = 0; j< N; ++ j)
{
sum_over_i [j] + = wij [i,j] / sum_over_j_wij [i]];
}
}
I have C code which computes the row sums of a matrix, divide each
element of each row with the row sum and then compute the column sum of
the resulting matrix. Is there a way I can speed up the code in C:
/* Here is the code */
// Table is "wij"
int i, j;
for(i = 0; i < N; ++i)
{
for(j = 0; j < N; ++j)
{
sum_over_j_wij[i] += wij[i,j];
}
for(j = 0; j < N; ++j)
{
sum_over_i[j] += wij[i,j]/sum_over_j_wij[i)];
}
}
1.如果你在说C,你为什么要发帖给clc ++?
2.你的代码不会编译; x [i,j]可能是语法错误
2.a.您的代码可能会编译,但它可能只提供x [j](或x [i],I
不记得逗号操作符是否在C)中返回右侧或左侧
操作数。
1. If you''re talking C, why are you posting to c.l.c++?
2. Your code won''t compile; x[i,j] may be a syntax error
2.a.Your code might compile, but it may only provide x[j] ( or x[i], I
don''t remember whether the comma operator return the right or left
operand in C).
这篇关于加快C代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!