星期二, 六月 24, 2003

STL性能分析-map与hash_map

hash_map并不属于C++标准, 但是众多的编译器提供商都提供了自己的hash_map的实现, 我们知道hash_map相对map的最大优势是其O(1)的访问时间, 本文使用一个简单的测试来说明map与hash_map实际的性能差异, 并同时给出MS实现STL(VC6)与SGI实现STL在map上的性能差异.
用于测试的小程序完成一段文本的交叉索引的制作, 使用STL可以使得程序相当简化. 下面是使用map的版本, 测试文本使用了MySql 3.23.16的帮助文档manual.txt(1969K).

#include <>
#include <>
#include <>
#include <>
#include <>
#include <>
#include <>
struct Compare {
bool operator()(std::string a, std::string b) const {
return a <>, Compare > MAP;
MAP CrossRef;
char c;
int LineNo = 1;
time_t starttime,endtime;
time(&starttime);

while(Source) {
c = '\0';
while(Source && !(isalpha(c) || '_' == c)) {
Source.get(c);
if(c == '\n') ++LineNo;
}
string aKey(1,c);
while(Source && (isalnum(c) || '_' == c)) {
Source.get(c);
if(isalnum(c) || '_' == c)
aKey += c;
}
Source.putback(c);
if(c)
CrossRef[aKey].push_back(LineNo);
}
time(&endtime);
cout < < "time:" <<>
#include <>
#include <>
#include <>
#include <>
#include <>
#include <>
struct Compare {
bool operator()(std::string a, std::string b) const {
return a <> > MAP;
MAP CrossRef;
char c;
int LineNo = 1;
time_t starttime,endtime;
time(&starttime);

while(Source) {
c = '\0';
while(Source && !(isalpha(c) || '_' == c)) {
Source.get(c);
if(c == '\n') ++LineNo;
}
string aKey(1,c);
while(Source && (isalnum(c) || '_' == c)) {
Source.get(c);
if(isalnum(c) || '_' == c)
aKey += c;
}
Source.putback(c);
if(c)
CrossRef[aKey].push_back(LineNo);
}
time(&endtime);
cout < < "time:" <<>
#include <>
#include <>
#include <>
#include <>
#include <>
#include <>
struct Compare {
bool operator()(std::string a, std::string b) const {
return a <> > MAP;
MAP CrossRef;
char c;
int LineNo = 1;
time_t starttime,endtime;
time(&starttime);

while(Source) {
c = '\0';
while(Source && !(isalpha(c) || '_' == c)) {
Source.get(c);
if(c == '\n') ++LineNo;
}
string aKey(1,c);
while(Source && (isalnum(c) || '_' == c)) {
Source.get(c);
if(isalnum(c) || '_' == c)
aKey += c;
}
Source.putback(c);
//if(c)
//CrossRef[aKey].push_back(LineNo);
}
time(&endtime);
cout << "time:" << endtime - starttime << '\n';
}

测试结果:
DEBUG版本:
I/O时间: 6s
hash_map版本: 10s
SGI map版本: 24s
MS map版本: 26s
RELEASE版本(加大了测试文本到20M):
I/O时间: 3s
hash_map版本: 7s
SGI map版本: 14s
MS map版本: 18s

测试结果相当稳定, 连续多次测试, 结果完全一样, 从结果看, hash_map由于其O(1)的访问时间, 完全占据了性能优势, 可惜MS没有提供hash_map用于对比测试, 不过从map的实现上看, SGI的实现也是明显优于MS实现, 快了10%~30%左右.

星期日, 六月 01, 2003

Free 3D引擎

3D引擎是3D游戏/应用的核心, 从id引入的Quake引擎以来, 3D引擎得到迅速的发展, 目前流行的3D引擎很多, 如Quake, Unreal, LithTech等等. 同时互联网上也出现了一些非常好的免费的3D引擎, 甚至是开放源码的引擎, 这些开放源码的引擎有些也达到相当高的技术水平, 甚至在某些商业游戏中得以应用, 本文将会介绍几款流行的免费3D引擎.
1)Crystal Space 最为流行的开放源码3D引擎
Crystal Space是广为流行的3D引擎, 并且仍在不断发展中, 在本文描述的4个引擎中, Crystal Space要算得上巨无霸级别了, 它几乎包括了3D游戏所需的各个方面. 并且Crystal Space从其他OpenSource项目吸收了很多好的东西, 可以算上集众家之长.
技术: 4
功能: 5
性能: 5
文档: 5
支持: 4

2)Ogre
Ogre是面向对象图象渲染引擎的缩写, 确切的说Ogre是3D渲染引擎而不是游戏引擎. 因为它缺乏对音乐, 网络, 输入等部分的支持.
技术: 3
功能: 3
性能:
文档: 5
支持: 2


3)Fly3D http://www.fly3d.com.br/
Fly3D是一个小型3D游戏引擎, 其独具创意的插件系统使得使用Fly3D制作游戏只需为Fly3D写一个插件即可, 你甚至可以不用写一行代码就可以完成一个完整的游戏框架!

4)Genesis3D http://www.genesis3d.com/
Genesis3D