Table of Contents
Question content
Solution
Home Backend Development Golang Why is reading and writing files in Go so much slower than Perl?

Why is reading and writing files in Go so much slower than Perl?

Feb 09, 2024 pm 09:30 PM
standard library

为什么 Go 中读写文件比 Perl 慢很多?

Why is reading and writing files in Go much slower than Perl? This is a common problem that many developers encounter when using these two programming languages. In this article, PHP editor Strawberry will answer this question for you. When comparing the speed of reading and writing files between Go and Perl, we need to consider two key factors: language features and underlying implementation. The design philosophy of the Go language in terms of file reading and writing is different from that of Perl, which leads to differences in performance. At the same time, the underlying implementation is also an important factor affecting the reading and writing speed. Next, we will analyze these factors in detail to help you better understand why reading and writing files in Go is much slower than Perl.

Question content

I use go to improve code efficiency, but when I use go to read and write files, I find that its reading and writing efficiency is not as high as perl. Is it a problem with my code or other reasons?

Build input file:

1

2

# input file:

for i in $(seq 1 600000) do     echo server$((random%800+100)),$random,$random,$random >> sample.csv done

Copy after login

Read and write files using perl:

1

time cat sample.csv | perl -ne 'chomp;print"$_"' > out.txt

Copy after login

1

2

3

real    0m0.249s

user    0m0.083s

sys 0m0.049s

Copy after login

Use go to read and write files:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

package main

 

import (

    "bufio"

    "fmt"

    "io"

    "os"

    "strings"

)

 

func main() {

 

    filepath := "./sample.csv"

    file, err := os.openfile(filepath, os.o_rdwr, 0666)

    if err != nil {

        fmt.println("open file error!", err)

        return

    }

    defer file.close()

    buf := bufio.newreader(file)

    for {

        line, err := buf.readstring('\n')

        line = strings.trimspace(line)

        fmt.println(line)

        if err != nil {

            if err == io.eof {

                fmt.println("file read ok!")

                break

            } else {

                fmt.println("read file error!", err)

                return

            }

        }

    }

}

Copy after login

Then I run:

1

time go run read.go > out.txt

Copy after login

1

2

3

real    0m2.332s

user    0m0.326s

sys 0m2.038s

Copy after login

Why is the read and write speed of go nearly 10 times slower than perl?

Solution

You are comparing apples to oranges.

There are at least two method errors:

  1. Your perl spell measures cat How to read a file and send its contents via pipe(2) while perl Read the data from there, process it and write the results to its standard output.

  2. Your Go spell

    • Measure the complete build process of the go tool chain (including compiling, linking and writing out the executable image file) Then run components of a compiled program, and
    • Measures unbuffered writes to stdout (fmt.print* calls) while writing to stdout in perl code - Quoting Documentation - "If Output to the terminal, usually line buffered, otherwise block buffered."

Let’s try to compare apples to apples.

First, here is a similar go implementation:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

package main

 

import (

    "bufio"

    "bytes"

    "fmt"

    "os"

)

 

func main() {

    in := bufio.newscanner(os.stdin)

    out := bufio.newwriter(os.stdout)

 

    for in.scan() {

        s := bytes.trimspace(in.bytes())

 

        if _, err := out.write(s); err != nil {

            fmt.fprint(os.stderr, "failed to write file:", err)

            os.exit(1)

        }

    }

 

    if err := out.flush(); err != nil {

        fmt.fprint(os.stderr, "failed to write file:", err)

        os.exit(1)

    }

 

    if err := in.err(); err != nil {

        fmt.fprint(os.stderr, "reading failed:", err)

        os.exit(1)

    }

}

Copy after login

Let’s save it as chomp.go and measure it:

  1. Build code:

    $ go build chomp.go

  2. Generate input file:

    $ for i in $(seq 1 600000);Execute echo server$((random�0 100)),$random,$random,$random;Complete>sample.csv

  3. Run perl code:

    1

    2

    3

    4

    5

    $ time { perl -ne 'chomp; print "$_";' <sample.csv >out1.txt; }

     

    real    0m0.226s

    user    0m0.102s

    sys 0m0.048s

    Copy after login
  4. Run it again to make sure it has read the input file from the file system cache:

    1

    2

    3

    4

    5

    $ time { perl -ne 'chomp; print "$_";' <sample.csv >out1.txt; }

     

    real   0m0.123s

    user   0m0.090s

    sys    0m0.033s

    Copy after login

    Notice how the execution time is reduced.

  5. Run go code on cached input:

    1

    2

    3

    4

    5

    $ time { ./chomp <sample.csv >out2.txt; }

     

    real   0m0.063s

    user   0m0.032s

    sys    0m0.032s

    Copy after login
  6. Make sure the results are the same:

    $ cmp out1.txt out2.txt

  7. As you can see, on my linux/amd64 system with an ssd, the results are roughly the same.

    Well I should also point out that in order to get reasonable results you would need to run each command say 1000 times and average the results in each batch and then compare the numbers, but I think this is enough to prove What is the problem with your approach.

    One more thing to consider: the running time of both programs is overwhelmingly dominated by filesystem i/o, so if you think go will be faster, your expectations are unfounded: both Most of the time, the program sleep calls read(2) and write(2) in the kernel system. A go program might be faster than a perl program in some cases involving cpu operations (especially if it's written to take advantage of a multi-core system), but that's not the case at all with your example.

    Oh, just to clarify the unstated fact: although the go language specification does not specify aot, and go run is a hack for one-time one-time gigs, No Serious work, nor execution of code of any serious complexity. In short, go-that-you-are-using is not an interpreted language, although the availability of go run may make it appear so. In fact, it does what a normal go build would do and then runs the resulting executable and then discards it.

    You might be tempted to say that perl also handles "source code", but the perl interpreter is highly optimized for handling scripts and go's build toolchain - while being incredibly fast compared to most other compiled languages ​​- Not optimized for this.
    Perhaps the more obvious difference is that the perl interpreter actually interprets your (very simple) script, whereas chomp and print are so-called "built-in functions ”, easily provided for script execution by the interpreter. In contrast to building a go program, where the compiler parses the source code files and converts them into machine code, the linker actually reads the files for the compiled package of the go standard library - all of which are imported, - From them, combine all this machine code and write out an executable image file (which is much like the perl binary itself!); of course this is a very resource-intensive process that has nothing to do with the actual program execution .

    The above is the detailed content of Why is reading and writing files in Go so much slower than Perl?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use absolute value in c++ How to use absolute value in c++ May 06, 2024 pm 06:15 PM

There are two ways to obtain absolute values ​​in C++: 1. Use the built-in function abs() to obtain the absolute value of an integer or floating point type; 2. Use the generic function std::abs() to obtain various supported absolute values. Operates on absolute values ​​of data types.

How to use std:: in c++ How to use std:: in c++ May 09, 2024 am 03:45 AM

std is the namespace in C++ that contains components of the standard library. In order to use std, use the "using namespace std;" statement. Using symbols directly from the std namespace can simplify your code, but is recommended only when needed to avoid namespace pollution.

What does prime mean in c++ What does prime mean in c++ May 07, 2024 pm 11:33 PM

prime is a keyword in C++, indicating the prime number type, which can only be divided by 1 and itself. It is used as a Boolean type to indicate whether the given value is a prime number. If it is a prime number, it is true, otherwise it is false.

What does fabs mean in c++ What does fabs mean in c++ May 08, 2024 am 01:15 AM

The fabs() function is a mathematical function in C++ that calculates the absolute value of a floating point number, removes the negative sign and returns a positive value. It accepts a floating point parameter and returns an absolute value of type double. For example, fabs(-5.5) returns 5.5. This function works with floating point numbers, whose accuracy is affected by the underlying hardware.

What does config mean in java? What does config mean in java? May 07, 2024 am 02:39 AM

Config represents configuration information in Java and is used to adjust application behavior. It is usually stored in external files or databases and can be managed through Java Properties, PropertyResourceBundle, Java Configuration Framework or third-party libraries. Its benefits include decoupling and flexibility. , environmental awareness, manageability, scalability.

_complex usage in c language _complex usage in c language May 08, 2024 pm 01:27 PM

The complex type is used to represent complex numbers in C language, including real and imaginary parts. Its initialization form is complex_number = 3.14 + 2.71i, the real part can be accessed through creal(complex_number), and the imaginary part can be accessed through cimag(complex_number). This type supports common mathematical operations such as addition, subtraction, multiplication, division, and modulo. In addition, a set of functions for working with complex numbers is provided, such as cpow, csqrt, cexp, and csin.

What does min mean in c++ What does min mean in c++ May 08, 2024 am 12:51 AM

The min function in C++ returns the minimum of multiple values. The syntax is: min(a, b), where a and b are the values ​​to be compared. You can also specify a comparison function to support types that do not support the < operator. C++20 introduced the std::clamp function, which handles the minimum of three or more values.

How to calculate absolute value in c++ How to calculate absolute value in c++ May 06, 2024 pm 06:21 PM

There are three ways to find the absolute value in C++: Using the abs() function, you can calculate the absolute value of any type of number. Using the std::abs() function, you can calculate the absolute value of integers, floating point numbers, and complex numbers. Manual calculation of absolute values, suitable for simple integers.

See all articles