私は、より高速のバイナリファイルリーダーを研究していました。C++のifstream :: readまたはCのフリーダです。パフォーマンスの比較 - pcapファイルの読み込み:C++のifstream VS Cのフリーダイヤル
インターネットによれば、類似の質問を含むは、それほど違いはないので、私はディッパを掘ることにしました。
私は1.22ギガバイトのpcapファイルを使用しました。このファイルには、約1,377,000パケットが含まれています。 どちらのプログラムもmingw32-g ++を使用してコンパイルされています。
ヘッダ構造体は、Wiresharkののウィキに従って定義される - のlibpcapファイル構造は: https://wiki.wireshark.org/Development/LibpcapFileFormat
これは、Cコードである:
#include <stdio.h>
#include <stdlib.h>
#include <Winsock2.h>
/* definition of structs: pcap_global_header, pcap_packet_header, ethernet_header, ipv4_header, tcp_header */
int main()
{
int count = 0, bytes_read;
/* open file */
FILE * file = fopen("test.pcap", "rb");
/* read file header */
struct pcap_global_header gheader;
fread(&gheader, sizeof(char), sizeof(struct pcap_global_header), file);
// if not ethernet type
if(gheader.network != 1)
{
printf("not ethernet !\n");
return 1;
}
/* read packets */
char *buffer = (char*)malloc(gheader.snaplen);
struct pcap_packet_header pheader;
struct ether_header eth;
struct ipv4_header ip;
struct tcp_header tcp;
fread(&pheader, sizeof(char), sizeof(struct pcap_packet_header), file);
while(!feof(file))
{
++count;
bytes_read = fread(ð, sizeof(char), sizeof(struct ether_header), file);
// ip
if(eth.type == 0x08)
{
bytes_read += fread(&ip, sizeof(char), sizeof(struct ipv4_header), file);
//tcp
if(ip.protocol == 0x06)
{
bytes_read += fread(&tcp, sizeof(char), sizeof(struct tcp_header), file);
}
}
//read rest of the packet
fread(buffer, sizeof(char), pheader.incl_len - bytes_read, file);
// read next packet's header
fread(&pheader, sizeof(char), sizeof(struct pcap_packet_header), file);
}
printf("(C) total packets: %d\n", count);
return 0;
}
これはC++コードである:
#include <iostream>
#include <fstream>
#include <memory>
#include <Winsock2.h>
/* definition of structs: pcap_global_header, pcap_packet_header, ethernet_header, ipv4_header, tcp_header */
int main()
{
int count_packets = 0, bytes_read;
/* open file */
std::ifstream file("test.pcap", std::fstream::binary | std::fstream::in);
/* read file header */
struct pcap_global_header gheader;
file.read((char*)&gheader, sizeof(struct pcap_global_header));
// if not ethernet type
if(gheader.network != 1)
{
printf("not ethernet !\n");
return 1;
}
/* read packets */
char *buffer = std::allocator<char>().allocate(gheader.snaplen);
struct pcap_packet_header pheader;
struct ether_header eth;
struct ipv4_header ip;
struct tcp_header tcp;
file.read((char*)&pheader, sizeof(pcap_packet_header));
while(!file.eof())
{
++count_packets;
file.read((char*)ð, sizeof(struct ether_header));
bytes_read = sizeof(struct ether_header);
// ip
if(eth.type == 0x08)
{
file.read((char*)&ip, sizeof(struct ipv4_header));
bytes_read += sizeof(struct ipv4_header);
//tcp
if(ip.protocol == 0x06)
{
file.read((char*)&tcp, sizeof(struct tcp_header));
bytes_read += sizeof(struct tcp_header);
}
}
// read rest of the packet
file.read(buffer, pheader.incl_len - bytes_read);
// read next packet's header
file.read((char*)&pheader, sizeof(pcap_packet_header));
}
std::cout << "(C++) total packets :" << count_packets << std::endl;
return 0;
}
結果は非常に残念です:
Cコードの結果:
(C) total packets: 1377065
Process returned 0 (0x0) execution time : 1.031 s
Press any key to continue.
C++コードの結果:
明らか(C++) total packets :1377065
Process returned 0 (0x0) execution time : 3.172 s
Press any key to continue.
、私は各バージョンを数回を走り、そう、私はC++を使用してファイルを読み込むためのより高速な方法を探しています。
*私はC++を使ってファイルを読むより速い方法を探しています*あなたはそれを見つけました - ':: fread()'を使ってください。また、[while(!feof(file))が常に間違っているのはなぜですか?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) –
最適化されていません?最適化をしないと、これをベンチマークするのはなぜですか? – Banex
@AndrewHenle私はfeof()を間違って使用していますか? –