平台:Visual C++6.0
#include
"
stdafx.h
"
#include < time.h >
#include < stdio.h >
int main( int argc, char * argv[])
... {
for ( int cnt=0; cnt < 1000000; cnt++ )
...{
time_t start;
time(&start);
for ( int i = 0; i < 1; i++ )
;
time_t end;
time(&end);
if( end > start)
printf("%d",end-start);
}
return 0;
}
#include < time.h >
#include < stdio.h >
int main( int argc, char * argv[])
... {
for ( int cnt=0; cnt < 1000000; cnt++ )
...{
time_t start;
time(&start);
for ( int i = 0; i < 1; i++ )
;
time_t end;
time(&end);
if( end > start)
printf("%d",end-start);
}
return 0;
}
运行五次
0
11
1
1
0
将循环次数改为10000000,运行五次,结果分别为:
1111111
11111
11111111111
111111111
11111111
平台:Eclipse3.2 ,JDK1.5
public
class
Test
implements
Runnable
... {
public static void main(String[] args)
...{
Thread tThread = new Thread(new Test());
tThread.setPriority(Thread.MAX_PRIORITY);
tThread.start();
}
public void run()
...{
int cnt = 1;
long MAX_VALUE = 1000000;
while( cnt < MAX_VALUE )
...{
long start = System.currentTimeMillis();
for ( int i = 0; i < 1; i++ )
;
long end = System.currentTimeMillis();
if( end > start )
System.out.print((end-start)+"/t");
cnt++;
// System.gc();
}
}
}
... {
public static void main(String[] args)
...{
Thread tThread = new Thread(new Test());
tThread.setPriority(Thread.MAX_PRIORITY);
tThread.start();
}
public void run()
...{
int cnt = 1;
long MAX_VALUE = 1000000;
while( cnt < MAX_VALUE )
...{
long start = System.currentTimeMillis();
for ( int i = 0; i < 1; i++ )
;
long end = System.currentTimeMillis();
if( end > start )
System.out.print((end-start)+"/t");
cnt++;
// System.gc();
}
}
}
运行五次,结果依次为:
16
15
15
16
15
16
16 15 16 15 16 16
16 15 15 15
15 16 16 15 16
15 16 15 15 16 15
16 15 16 15 16 16
16 15 15 15
15 16 16 15 16
15 16 15 15 16 15
倘若 循环最末端 加上 System.gc(); ,整个代码段运行时间会大大增加(10倍以上),但输出可能性大大减少。
注意,这里用了线程,且是最高优先级。但用普通优先级与最高优先级获得的效果似乎一样的。
平台:Visual Studio 2005, C#
using
System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
namespace TestApplication
... {
class Program
...{
static void Main(string[] args)
...{
test();
}
public static void test()
...{
int cnt = 0;
int cc = 0;
while (cnt < 1000000)
...{
int startTime = DateTime.Now.Millisecond;
for (int i = 0; i < 1; i++)
;
int endTime = DateTime.Now.Millisecond;
if (endTime > startTime)
cc++;
// Console.Write( (endTime - startTime)+" " );
cnt++;
}
Console.WriteLine(cc);
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Timers;
namespace TestApplication
... {
class Program
...{
static void Main(string[] args)
...{
test();
}
public static void test()
...{
int cnt = 0;
int cc = 0;
while (cnt < 1000000)
...{
int startTime = DateTime.Now.Millisecond;
for (int i = 0; i < 1; i++)
;
int endTime = DateTime.Now.Millisecond;
if (endTime > startTime)
cc++;
// Console.Write( (endTime - startTime)+" " );
cnt++;
}
Console.WriteLine(cc);
}
}
}
由于输出量太大了,所以没办法一个个写出来,只好计数。运行五次,计数如下:
48
43
34
46
40
平台Matlab:
n
=
0
;
for i = 1 : 1000000
tic
for i = 1 : 1
;
end
ct = toc ;
if( ct> 0 )
n = n+ 1 ;
end
end
n
for i = 1 : 1000000
tic
for i = 1 : 1
;
end
ct = toc ;
if( ct> 0 )
n = n+ 1 ;
end
end
n
输出量太大,因此改为计数,运行五次,计数如下:
240
227
210
215
225