From 9a463d4675fbf415e2173c4e537a2cf54a952d89 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 2 Feb 2018 12:09:22 +1100 Subject: [PATCH] Add some more Go and Rust GIF benchmarks On my machine (Debian Testing as of November 2017, Intel x86_64 Broadwell): Rust 28.0 rustc 1.14 github.com/Geal/gif.rs Rust 29.1 rustc 1.14 github.com/PistonDevelopers/image-gif Go 62.7 go 1.9 standard library's image/gif C 97.3 giflib 5.1 from Debian Testing's /usr/lib Wuffs 123 clang 5.0 Wuffs 136 gcc 7.2 Numbers are megabytes per second, equivalent to megapixels per second for GIF images. --- script/bench-go-gif/main.go | 95 ++++++++++++++++++++++++ script/bench-rust-gif-dot-rs/src/main.rs | 34 +++++++-- script/bench-rust-gif/Cargo.toml | 7 ++ script/bench-rust-gif/src/main.rs | 93 +++++++++++++++++++++++ 4 files changed, 222 insertions(+), 7 deletions(-) create mode 100644 script/bench-go-gif/main.go create mode 100644 script/bench-rust-gif/Cargo.toml create mode 100644 script/bench-rust-gif/src/main.rs diff --git a/script/bench-go-gif/main.go b/script/bench-go-gif/main.go new file mode 100644 index 00000000..ae0ef96e --- /dev/null +++ b/script/bench-go-gif/main.go @@ -0,0 +1,95 @@ +// Copyright 2018 The Wuffs Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build ignore + +package main + +// This program exercises the Go standard library's GIF decoder. +// +// Wuffs' C code doesn't depend on Go per se, but this program gives some +// performance data for specific Go GIF implementations. The equivalent Wuffs +// benchmarks (on the same test image) are run via: +// +// wuffs bench -mimic -focus=wuffs_gif_decode_1000k,mimic_gif_decode_1000k std/gif +// +// The "1000k" is because the test image (harvesters.gif) has approximately 1 +// million pixels. +// +// The Wuffs benchmark reports megabytes per second. This program reports +// megapixels per second. The two concepts should be equivalent, since GIF +// images' pixel data are always 1 byte per pixel indices into a color palette. + +import ( + "bytes" + "fmt" + "image" + "image/gif" + "io/ioutil" + "os" + "time" +) + +// These constants are hard-coded to the harvesters.gif test image. +const WIDTH = 1165 +const HEIGHT = 859 +const BYTES_PER_PIXEL = 1 // Palette index. +const NUM_BYTES = WIDTH * HEIGHT * BYTES_PER_PIXEL +const FIRST_PIXEL = 0 // Top left pixel's palette index is 0x00. +const LAST_PIXEL = 1 // Bottom right pixel's palette index is 0x01. + +func main() { + if err := main1(); err != nil { + os.Stderr.WriteString(err.Error() + "\n") + os.Exit(1) + } +} + +func main1() error { + src, err := ioutil.ReadFile("../../test/testdata/harvesters.gif") + if err != nil { + return err + } + + start := time.Now() + + const REPS = 50 + for i := 0; i < REPS; i++ { + decode(src) + } + + elapsedNanos := time.Since(start) + + totalPixels := uint64(WIDTH * HEIGHT * REPS) + kpPerS := totalPixels * 1000000 / uint64(elapsedNanos) + + fmt.Printf("Go %3d.%03d megapixels/second\n", kpPerS/1000, kpPerS%1000) + return nil +} + +func decode(src []byte) { + img, err := gif.Decode(bytes.NewReader(src)) + if err != nil { + panic(err) + } + m := img.(*image.Paletted) + if len(m.Pix) != NUM_BYTES { + panic("wrong num_bytes") + } + + // A hard-coded sanity check that we decoded the pixel data correctly. + if (m.Pix[0] != FIRST_PIXEL) || (m.Pix[NUM_BYTES-1] != LAST_PIXEL) { + panic("wrong dst pixels") + } +} diff --git a/script/bench-rust-gif-dot-rs/src/main.rs b/script/bench-rust-gif-dot-rs/src/main.rs index c7bb8705..4bdf7f4d 100644 --- a/script/bench-rust-gif-dot-rs/src/main.rs +++ b/script/bench-rust-gif-dot-rs/src/main.rs @@ -12,16 +12,36 @@ // See the License for the specific language governing permissions and // limitations under the License. -// This program exercises the Rust GIF decoder in https://github.com/Geal/gif.rs +// This program exercises the Rust GIF decoder at +// https://github.com/Geal/gif.rs // -// Wuffs doesn't depend on Rust or Nom (a parser combinator library written in -// Rust) per se, but this program gives some comparitive data on how fast a -// Rust GIF decoder could perform. +// Wuffs' C code doesn't depend on Rust or Nom (a parser combinator library +// written in Rust) per se, but this program gives some performance data for +// specific Rust GIF implementations. The equivalent Wuffs benchmarks (on the +// same test image) are run via: +// +// wuffs bench -mimic -focus=wuffs_gif_decode_1000k,mimic_gif_decode_1000k std/gif +// +// The "1000k" is because the test image (harvesters.gif) has approximately 1 +// million pixels. +// +// The Wuffs benchmark reports megabytes per second. This program reports +// megapixels per second. The two concepts should be equivalent, since GIF +// images' pixel data are always 1 byte per pixel indices into a color palette. +// However, the gif.rs library only lets you decode 3 bytes per pixel (RGB), +// not 1 byte per pixel. This automatic palette look-up is arguably a defect in +// its API: decoding a 1,000 × 1,000 pixel image requires (2,000,000 - 768) +// more bytes this way, per frame, and animated GIFs have multiple frames. // // To run this program, do "cargo run --release > /dev/null" from the parent // directory (the directory containing the Cargo.toml file). See the eprint // comment below for why we redirect to /dev/null. +// TODO: unify this program, bench-rust-gif-dot-rs, with bench-rust-gif, the +// other Rust GIF benchmark program. They are two separate programs because +// both libraries want to be named "gif", and the cargo package manager cannot +// handle duplicate names (https://github.com/rust-lang/cargo/issues/1311). + extern crate gif; use std::time::Instant; @@ -62,7 +82,7 @@ fn main() { // https://github.com/Geal/gif.rs/issues/4). When running this program, it // can be useful to redirect stdout (but not stderr) to /dev/null. eprint!( - "{}.{:03} megapixels/second\n", + "gif.rs {:3}.{:03} megapixels/second github.com/Geal/gif.rs\n", kp_per_s / 1_000, kp_per_s % 1_000 ); @@ -70,8 +90,8 @@ fn main() { fn decode(dst: &mut [u8], src: &[u8]) { // Set up the hard-coded sanity check, executed below. - dst[0] = 0; - dst[NUM_BYTES - 1] = 0; + dst[0] = 0xFE; + dst[NUM_BYTES - 1] = 0xFE; let (_, colors) = gif::parser::color_table(&src[COLOR_TABLE_OFFSET..], COLOR_TABLE_ELEMENT_COUNT).unwrap(); diff --git a/script/bench-rust-gif/Cargo.toml b/script/bench-rust-gif/Cargo.toml new file mode 100644 index 00000000..609f5902 --- /dev/null +++ b/script/bench-rust-gif/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "bench_rust_gif" +version = "0.0.1" +authors = ["Nigel Tao "] + +[dependencies] +gif = "*" diff --git a/script/bench-rust-gif/src/main.rs b/script/bench-rust-gif/src/main.rs new file mode 100644 index 00000000..47d7d596 --- /dev/null +++ b/script/bench-rust-gif/src/main.rs @@ -0,0 +1,93 @@ +// Copyright 2018 The Wuffs Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// This program exercises the Rust GIF decoder at +// https://github.com/PistonDevelopers/image-gif +// +// Wuffs' C code doesn't depend on Rust per se, but this program gives some +// performance data for specific Rust GIF implementations. The equivalent Wuffs +// benchmarks (on the same test image) are run via: +// +// wuffs bench -mimic -focus=wuffs_gif_decode_1000k,mimic_gif_decode_1000k std/gif +// +// The "1000k" is because the test image (harvesters.gif) has approximately 1 +// million pixels. +// +// The Wuffs benchmark reports megabytes per second. This program reports +// megapixels per second. The two concepts should be equivalent, since GIF +// images' pixel data are always 1 byte per pixel indices into a color palette. +// +// To run this program, do "cargo run --release" from the parent directory (the +// directory containing the Cargo.toml file). + +// TODO: unify this program, bench-rust-gif-dot-rs, with bench-rust-gif, the +// other Rust GIF benchmark program. They are two separate programs because +// both libraries want to be named "gif", and the cargo package manager cannot +// handle duplicate names (https://github.com/rust-lang/cargo/issues/1311). + +extern crate gif; + +use std::time::Instant; + +// These constants are hard-coded to the harvesters.gif test image. +const WIDTH: usize = 1165; +const HEIGHT: usize = 859; +const BYTES_PER_PIXEL: usize = 1; // Palette index. +const NUM_BYTES: usize = WIDTH * HEIGHT * BYTES_PER_PIXEL; +const FIRST_PIXEL: u8 = 0; // Top left pixel's palette index is 0x00. +const LAST_PIXEL: u8 = 1; // Bottom right pixel's palette index is 0x01. + +fn main() { + let mut dst = [0; NUM_BYTES]; + let src = include_bytes!("../../../test/testdata/harvesters.gif"); + + let start = Instant::now(); + + const REPS: u32 = 50; + for _ in 0..REPS { + decode(&mut dst[..], src); + } + + let elapsed = start.elapsed(); + let elapsed_nanos = elapsed.as_secs() * 1_000_000_000 + (elapsed.subsec_nanos() as u64); + + let total_pixels: u64 = ((WIDTH * HEIGHT) as u64) * (REPS as u64); + let kp_per_s: u64 = total_pixels * 1_000_000 / elapsed_nanos; + + print!( + "gif {:3}.{:03} megapixels/second github.com/PistonDevelopers/image-gif\n", + kp_per_s / 1_000, + kp_per_s % 1_000 + ); +} + +fn decode(dst: &mut [u8], src: &[u8]) { + // Set up the hard-coded sanity check, executed below. + dst[0] = 0xFE; + dst[NUM_BYTES - 1] = 0xFE; + + let mut reader = gif::Decoder::new(src).read_info().unwrap(); + reader.next_frame_info().unwrap().unwrap(); + + if reader.buffer_size() != NUM_BYTES { + panic!("wrong num_bytes") + } + + reader.read_into_buffer(dst).unwrap(); + + // A hard-coded sanity check that we decoded the pixel data correctly. + if (dst[0] != FIRST_PIXEL) || (dst[NUM_BYTES - 1] != LAST_PIXEL) { + panic!("wrong dst pixels") + } +}