Using openmp to read text files in parallel takes longer than serial.

  c++, question

I want to use openmp to realize the parallel of string matching algorithm. I find that it takes a long time to read the text in C language. I want to optimize it, but it takes longer to read the file in parallel with openmp than the original serial program. I am very puzzled and ask for guidance.
4 threads used, parallel time 0.43s, serial time 0.39s There is no problem with the operation.
The number of threads running on the mac was entered at terminal when compiling, and no problem was found in the test.

long thread_count=strtol(argv[1],NULL,10);
 printf("%d\n",thread_count);
 FILE *fp=fopen("/Users/liuchong/Desktop/Harry Potter.txt","r");
 fseek(fp, 0L, SEEK_END);
 long SIZE = ftell(fp);
 fseek(fp, 0L, SEEK_SET);
 char *text = (char*) malloc(sizeof(char) * SIZE);
 
 if(!  fp)
 {
 printf("can't open file\n");
 return -1;
 }
 # pragma omp parallel for num_threads(thread_count)
 
 for (int i=0;   i<thread_count;  i++)
 {
 fseek(fp,i*SIZE/thread_count,SEEK_SET);
 
 long local_test=i*(SIZE/thread_count);
 
 for(local_test;  local_test<((i+1)*SIZE/thread_count);  local_test++)
 {
 fscanf(fp,"%c",&text[local_test]);
 }
 }
 
 
 //serial program
 /*
 for(long j=0;  j<SIZE;  j++)
 //while(!  feof(fp))
 {
 fscanf(fp,"%c",&text[test]);
 test++;
 
 //printf("%s",text);
 
 }
 */

Don’t look at the rest, look here alone.

for(long j=0;  j<SIZE;  j++)
 {
 fscanf(fp,"%c",&text[test]);
 test++;
 }

What you want is that this piece of code can run in parallel so as to speed up the reading speed.
I can’t achieve this goal here, because I dare not jump to conclusions.
It is used here.fscanfWhen compiling, if you are compiling a multi-threaded version of the program, this function will be locked to ensure its thread safety.
If you are not compiling a multithreaded program, you will not lock it. This isgccThis is the case in China, VS I think it should be the same. It takes time to lock and unlock, so it is normal to be slow.