可以用clock()函数, 返回自程序开始运行所经过的时间,返回值为clock_t , 转换返回值为秒, 返回值除以CLOCKS_PER_SEC.
#include
#include
int c = 0;
int main(int argc, char *argv[])
{
clock_t a = clock();
for(int i=0; i<9000 ; ++i )
{
for(int j=0; j<9000 ; ++j )
{
c++;
}
c -= 9000;
}
clock_t b = clock();
long double d = b - a;
long double time = d/CLOCKS_PER_SEC;
printf("time:%g", time);
return 0;
}
这个是微软的程序
#include
#include
#include
void sleep( clock_t wait );
void main( void )
{
long i = 600000L;
clock_t start, finish;
double duration;
/* Delay for a specified time. */
printf( "Delay for three seconds\n" );
sleep( (clock_t)3 * CLOCKS_PER_SEC );
printf( "Done!\n" );
/* Measure the duration of an event. */
printf( "Time to do %ld empty loops is ", i );
start = clock();
while( i-- )
;
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%2.1f seconds\n", duration );
}
/* Pauses for a specified number of milliseconds. */
void sleep( clock_t wait )
{
clock_t goal;
goal = wait + clock();
while( goal > clock() )
;
}
我想你的simulink从开始执行到调用该段M文件这个点总有一个切换点吧,那么用这个切换点在去切换switch,开始的时候是1,后面变为0,输出到workspace里面保存,假设变量名称为a。最后用M文件处理一下。
b=a.*t;
c(1)=b(1);
for i=2:length(a)
if b(i)>0
c(i)=b(i);
end
end
最后得到的c就是时间t为simulink开始执行到调用该段M文件之前的所有时间的点。