88 lines
1.9 KiB
Markdown
88 lines
1.9 KiB
Markdown
# WallTime - Quick Start Guide
|
|
|
|
## Sử dụng nhanh
|
|
|
|
### 1. Include header
|
|
|
|
```cpp
|
|
#include <robot/walltime.h>
|
|
```
|
|
|
|
### 2. Đo thời gian thực thi
|
|
|
|
```cpp
|
|
robot::WallTime start = robot::WallTime::now();
|
|
// ... your code ...
|
|
robot::WallDuration elapsed = robot::WallTime::now() - start;
|
|
std::cout << "Took " << elapsed.toSec() << " seconds" << std::endl;
|
|
```
|
|
|
|
### 3. Timeout
|
|
|
|
```cpp
|
|
robot::WallTime deadline = robot::WallTime::now() + robot::WallDuration(5.0);
|
|
while (robot::WallTime::now() < deadline) {
|
|
// Do work
|
|
}
|
|
```
|
|
|
|
### 4. Sleep
|
|
|
|
```cpp
|
|
// Sleep for 1 second
|
|
robot::WallDuration(1.0).sleep();
|
|
|
|
// Sleep until specific time
|
|
robot::WallTime target = robot::WallTime::now() + robot::WallDuration(5.0);
|
|
robot::WallTime::sleepUntil(target);
|
|
```
|
|
|
|
## Đặc điểm
|
|
|
|
- ✅ **Header-only**: Chỉ cần include một file
|
|
- ✅ **Không cần khởi tạo**: Dùng ngay
|
|
- ✅ **Thread-safe**: An toàn với multi-threading
|
|
- ✅ **Cross-platform**: Linux, Windows, macOS
|
|
|
|
## Tài liệu đầy đủ
|
|
|
|
- `WALLTIME_LIBRARY.md` - API reference đầy đủ
|
|
- `WALLTIME_USAGE.md` - Hướng dẫn chi tiết và ví dụ
|
|
- `examples/walltime_example.cpp` - Nhiều ví dụ thực tế
|
|
|
|
## So sánh với Time
|
|
|
|
| | `Time` | `WallTime` |
|
|
|---|---|---|
|
|
| Cần init | ✅ Có | ❌ Không |
|
|
| Simulated time | ✅ Có | ❌ Không |
|
|
| Dùng cho ROS messages | ✅ Nên | ❌ Không nên |
|
|
| Đo thời gian thực | ❌ Không phù hợp | ✅ Phù hợp |
|
|
|
|
## Ví dụ hoàn chỉnh
|
|
|
|
```cpp
|
|
#include <robot/walltime.h>
|
|
#include <iostream>
|
|
|
|
int main()
|
|
{
|
|
// Đo thời gian
|
|
robot::WallTime start = robot::WallTime::now();
|
|
|
|
// Simulate work
|
|
robot::WallDuration(0.5).sleep();
|
|
|
|
robot::WallDuration elapsed = robot::WallTime::now() - start;
|
|
std::cout << "Elapsed: " << elapsed.toSec() << "s" << std::endl;
|
|
|
|
return 0;
|
|
}
|
|
```
|
|
|
|
Compile:
|
|
```bash
|
|
g++ -std=c++17 -I./include your_file.cpp -o your_program
|
|
```
|
|
|