Вопрос по performance, java, multithreading, file-io, concurrency – Поможет ли использование нескольких потоков с RandomAccessFile повысить производительность?

5

Error: User Rate Limit ExceededRandomAccessFileError: User Rate Limit ExceededRandomAccessFileError: User Rate Limit Exceeded

Ваш Ответ

7   ответов
0

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededseagate.com/content/pdf/whitepaper/…
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
9

Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

1

Error: User Rate Limit Exceeded

3

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded drRoflol
Error: User Rate Limit ExceededsynchronousError: User Rate Limit Exceeded
13

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceededmultiple threads always speed up a program:

Update:Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

  • 1 Thread: 0.61s (0.61s)
  • 2 Threads: 0.44s (0.43s)
  • 4 Threads: 0.31s (0.28s) (Fastest)
  • 8 Threads: 0.53s (0.63s)

Error: User Rate Limit Exceeded

  • 1 Thread: 0.98s (1.01s)
  • 2 Threads: 0.67s (0.61s) (Fastest)
  • 4 Threads: 1.78s (0.63s)
  • 8 Threads: 2.06s (0.80s)

Error: User Rate Limit Exceeded

// FileReadThreads.cpp : Defines the entry point for the console application.
//

#include "Windows.h"
#include "stdio.h"
#include "conio.h"
#include <sys\timeb.h>
#include <io.h>    

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
int threadCount = 1;
char *fileName = 0;
int fileSize = 0;
double  GetSecs(void);

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

DWORD WINAPI FileReadThreadEntry(LPVOID lpThreadParameter)

{   char tx[255];

    int index = (int)lpThreadParameter;
    FILE *file = fopen(fileName, "rt");

    int start = (fileSize / threadCount) * index;
    int end   = (fileSize / threadCount) * (index + 1);

    fseek(file, start, SEEK_SET);

    printf("THREAD %4d started: Bytes %d-%d\n", GetCurrentThreadId(), start, end);


    for(int i = 0;; i++)
    {
        if(! fgets(tx, sizeof(tx), file))
            break;
        if(ftell(file) >= end)
            break;
    }
    fclose(file);

    printf("THREAD %4d done\n", GetCurrentThreadId());

    return 0;
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////



int main(int argc, char* argv[])
{
    if(argc <= 1)
    {
        printf("Usage:  <InputFile> <threadCount>\n");
        exit(-1);
    }

    if(argc > 2)
        threadCount = atoi(argv[2]);

    fileName = argv[1];
    FILE *file = fopen(fileName, "rt");
    if(! file)
    {
        printf("Unable to open %s\n", argv[1]);
        exit(-1);
    }

    fseek(file, 0, SEEK_END);
    fileSize = ftell(file);
    fclose(file);


    printf("Starting to read file %s with %d threads\n", fileName, threadCount);
    ///////////////////////////////////////////////////////////////////////////
    // Start threads
    ///////////////////////////////////////////////////////////////////////////
    double start = GetSecs();

    HANDLE mWorkThread[255];        

    for(int i = 0; i < threadCount; i++)
    {
        mWorkThread[i] = CreateThread(
                  NULL,
                  0,
                  FileReadThreadEntry,
                  (LPVOID) i,
                  0, 
                  NULL);
    }
    WaitForMultipleObjects(threadCount, mWorkThread, TRUE, INFINITE);

    printf("Runtime %.2f Secs\nDone\n", (GetSecs() - start) / 1000.);
    return 0;
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

double  GetSecs(void)

{
        struct timeb timebuffer;
        ftime(&timebuffer);
        return (double)timebuffer.millitm + 
              ((double)timebuffer.time * 1000.) - // Timezone needed for DbfGetToday
              ((double)timebuffer.timezone * 60. * 1000.);
}
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded drRoflol
Error: User Rate Limit Exceeded
1

Error: User Rate Limit ExceededperformanceError: User Rate Limit ExceededlatencyError: User Rate Limit ExceededthroughputError: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit Exceeded

3

RandomAccessFile is synchronised, so if you share an instance then you'll only have one thread running at one anyway.Error: User Rate Limit ExceededRandomAccessFileError: User Rate Limit Exceeded

Error: User Rate Limit ExceededRandomAccessFileError: User Rate Limit Exceeded

Error: User Rate Limit Exceededjava.nioError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded drRoflol

Похожие вопросы