큰 txt 파일 (100MB의 23 milion lines)이 있습니다.이 파일을 한 줄씩 열어서 linux에서 GNU shuf
명령처럼 섞으려고합니다. 나는 Windows 플랫폼에서 작업하고, 나는 Visual Studio 2015
을 설치하고 C++에서 프로그래밍을 시작합니다. 나는 오래된 C++ 코드를 사용하여 처음 시도했지만 너무 느리며 boost
라이브러리로 전환했다. 나는 그것을 인정해야한다, 그것은 정말로 빠르다. 그러나 나는 결과를 배열에 넣고 그것들을 섞는 것을 모른다 (배열은 100.000.000 인덱스까지 유지해야한다).텍스트 파일 읽기 및 셔플
이 내가
#include <boost/iostreams/device/mapped_file.hpp> // for mmap
#include <algorithm> // for std::find
#include <iostream> // for std::cout
#include <cstring>
#include <fstream>
#include <sstream>
#include <string>
int main()
{
boost::iostreams::mapped_file mmap("input.txt", boost::iostreams::mapped_file::readonly);
auto f = mmap.const_data();
auto l = f + mmap.size();
uintmax_t m_numLines = 0;
int inc1 = 0;
char ** ip = NULL;
boost::array<char, sizeof(int)> send_buf; <-- error here
/*
Severity Code Description Project File Line Suppression State
Error (active) namespace "boost" has no member "array" hshuffle c:\path_to_the\main.cpp 21
Severity Code Description Project File Line Suppression State
Error (active) type name is not allowed hshuffle c:\path_to_the\main.cpp 21
Severity Code Description Project File Line Suppression State
Error (active) identifier "send_buf" is undefined hshuffle c:\path_to_the\main.cpp 21
Severity Code Description Project File Line Suppression State
Error (active) a value of type "const char *" cannot be assigned to an entity of type "char *" hshuffle c:\path_to_the\main.cpp 29
*/
while (f && f != l)
{
if ((f = static_cast<const char*>(memchr(f, '\n', l - f))))
{
if ((m_numLines % 1000000) == 0)
{
ip[m_numLines] = l;
std::cout << m_numLines << "\n";
}
m_numLines++, f++;
}
}
std::cout << "m_numLines = " << m_numLines << "\n";
printf("endfille\n");
char a;
std::cin >> a;
}
OLD C++ 프로그램
puts("reading ips file [./i]");
if((fp=fopen("i","r")) == NULL)
{
printf("FATAL: Cant find i\n");
return -1;
}
int increment_ips = 0;
indIP = 0;
while (fgets(nutt,2024,fp))
{
while (t = strchr (nutt,'\n'))
*t = ' ';
temp = strtok (nutt, " ");
if (temp != NULL) {
string = strdup (temp);
indIP++;
while (temp = strtok (NULL, " "))
{
indIP++;
}
}
increment_ips++;
}
fclose(fp);
if((fp=fopen("i","r")) == NULL)
{
printf("FATAL: Cant find i\n");
return -1;
}
increment_ips = 0;
ip = new char*[indIP];
indIP = 0;
while (fgets(nutt,2024,fp))
{
while (t = strchr (nutt,'\n'))
*t = ' ';
temp = strtok (nutt, " ");
if (temp != NULL) {
string = strdup (temp);
ip[indIP++]=string;
while (temp = strtok (NULL, " "))
{
string = strdup (temp);
ip[indIP++]=string;
}
}
increment_ips++;
}
fclose(fp);
// shuffle
printf("Loaded [%d] ips\n",increment_ips);
puts("Shuffeling ips");
srand(time(NULL));
for(int i = 0; i <= increment_ips; i++)
{
int randnum = rand() % increment_ips + 1;
char* tempval;
tempval = ip[i];
ip[i] = ip[randnum];
ip[randnum] = tempval;
}
puts("Shuffeled");
모든 해결 방법을 시도해보십시오 무엇인가? 내가 preffer boost
따라서 정말 빠릅니다.
감사합니다.
당신은 얼마나 알고 싶어 배열에 임의의 정렬을 수행하려면 어떻게해야합니까? (bogo가 아니라 정말 무작위로 만듭니다.) – turoni
큰 텍스트 파일을 임의로 뒤섞어서 배열을 정의하고 거기에 변수를 저장하는 방법을 모르는 경우 배열에 100 만 개 이상의 줄이 있어야합니다. –
나 자신을 한 번도 해본 적이 없지만 나는 네가 최선이라고 생각한다. 그 많은 색인을 보유하는 [메모리 기반의 B + 나무] (http://stackoverflow.com/questions/1720738/looking-for-a-disk-based-b-tree-implementation-in-c-or-c). – turoni