之前我改了某网友的一个脚本,以此实现高亮c\c++里的一些关键字,如类名,linux下的一些函数等,后来发现索引生成的太慢,在我虚拟机里竟然要3分钟左右,于是我怀疑可能是vim脚本所致,后来我就用c++重写了一份,速度变为了只有半秒,秒杀vim脚本啊.
https://github.com/terryzhu/myconfiguration/blob/master/.vim/plugin/highligh.vim
http://code.google.com/p/jeffy-vim/
其实功能很简单,吧ctags生成的tags文件自己再解析一遍,放入一个hashmap,因为ctags文件里存着所有关键字,并且标明了此关键字是类名,还是方法名,还是define等等,于是要写代码解析出来.而在vim脚本里字符串都是直接拼接出来的,我怀疑效率问题就在此处.(会有数万次的字符串拼接)
let g:HLUDType = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ']for line in readfile(a:tagsfile)
" parse tag flag
let idx = stridx(line, ';"' . "\t")
if idx != -1
let s:flag = strpart(line, idx+3, 1)
" parse and save flag
let idx = index(s:HLUDFlag, s:flag)
if idx != -1
" slow in this line code
let g:HLUDType[idx] = g:HLUDType[idx] . matchstr(line, '^\<\h\w*\>') . ' '
endif
endif
endfor
(这问题关键就和Java里的String和StringBuilder的区别一样)
代码如下:
#include "iostream"
#include "string"
#include "vector"
#include "sstream"
#include <fstream>
#include "map"
// 木有注释,客官们将就着看吧,如有问题或improvement,我还会改进的
using namespace std;
int main() {
ifstream ifs;
ofstream ofs;
ofs.open("utags_bk");
string line;
std::map < int, string > dat;
ifs.open("tags");
while (std::getline(ifs, line)) {
string key = line.substr(0, line.find("\t"));
if (key.find("::") != -1) {
continue;
}
int type = line.find(";\"");
if (type == -1)
continue;
type = line[type + 3];
dat[type] += key + " ";
}
int taglist[] = { 'd', 'e', 'f', 'g', 'p', 's', 't', 'u', 'c' };
for (int i = 0; i < 9; i++) {
ofs << dat[taglist[i]] << endl;
}
ofs.flush();
ofs.close();
return 0;
}