Posts Tagged ‘programming’

clock() in C++

Sunday, August 5th, 2007

今天把在日照写的代码拿出来看了看,编译运行了一下,发生了一个意想不到的事:用来测时间的 clock() 函数返回了一个非常大的数……正常应该返回毫秒数啊。无奈,上网看了一下。看了一下 clock() 函数的介绍

Returns the number of clock ticks elapsed since the program was launched.

The macro constant expression CLOCKS_PER_SEC specifies the relation between a clock tick and a second (clock ticks per second).
The initial moment of reference used by clock as the beginning of the program execution may vary between platforms. To calculate the actual processing times of a program, the value returned by clock should be compared to a value returned by an initial call to clock.

原来返回的并不是毫秒数,而是一个number of clock,常量 CLOCKS_PER_SEC 定义了 clock 和秒数之间的关系,返回毫秒数应该用clock()*1000/CLOCKS_PER_SEC,而不是仅仅用 clock() 。Dev-C++自带的gcc3.4.2中CLOCKS_PER_SEC=1000,所以 clock() 正好返回毫秒数,我用的gcc4.1.2里CLOCKS_PER_SEC=1000000,返回的数当然大了。

今天又纠正了一个错误,不错。

PS:本来不想在这个 blog 里说关于 Programming 的东西的,想再开一个 blog ,现在懒得干了 ,不学 Programming 的同学们就不要看这些 post 了。

Tags:,,

Related Posts

Ten reasons why every programmer should learn C

Monday, November 27th, 2006

每个程序员都应该学习C的十个理由
引自:http://www.jubling.com/ten-reasons-why-every-programmer-should-learn-c.html
Every programmer should learn C during their programming career. Its benefits are to numerous to ignore. Not only will it open many more job opportunities, but it will teach you more about computers as a whole.

1) C is lower level then other programming languages (C++, Java). Programming at a lower level allows you to further your understanding of computers, as a whole.

2) Device drivers and operating systems are written exclusively in C. Now, you may never write a device driver or an operating system, but what if you are ever required to modify one?

3) What if you ever want to get a job programming microcontrollers? They are programmed in C. Are you going to limit your possible list of jobs because you didn't want to learn a new language?

4) C programs are smaller and faster then any other program created in a different language. Sometimes your program needs that speed boost that only C can give it.

5) If you have learned C, you can learn any modern programming language. The reason behind this is that all modern programming languages are based on C (Java, C++, C#, etc).

6) Because C has been around for many years, it has a large community and collective code base. This allows you to quickly and efficiently implement new algorithms or functions that have been programmed before.

7) C is the language of the Open Source community. The Open Source poster child, Linux, was coded in C. If you know C, you can participate in and contribute to numerous Open Source communities like Source Forge.

8) C is the only language that teaches you what pointers really are. C# and Java skip the subject completely. It is pointers that give C its power.

9) C is still the most commonly required language for programming jobs. It is well worth your time to get C under your belt.

10) Anything that has a microprocessor in it has support for C. From your microwave to your cell phone, C powers technology.

Tags:,,,,

Related Posts