;;; -*- mode: scheme; coding: iso-8859-1; -*-
;;; R6RS Byte Vectors.
;;;
;;; Copyright 2009, 2010 Free Software Foundation, Inc.
;;;
;;; This program is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Lesser General Public License
;;; as published by the Free Software Foundation; either version 3, or
;;; (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU Lesser General Public License for more details.
;;;
;;; You should have received a copy of the GNU Lesser General Public
;;; License along with this software; see the file COPYING.LESSER.  If
;;; not, write to the Free Software Foundation, Inc., 51 Franklin
;;; Street, Fifth Floor, Boston, MA 02110-1301 USA

(define-module (benchmarks bytevector)
  #:use-module (rnrs bytevectors)
  #:use-module (srfi srfi-4)
  #:use-module (benchmark-suite lib))

(define bv (make-bytevector 16384))

(define %native-endianness
  (native-endianness))

(define %foreign-endianness
  (if (eq? (native-endianness) (endianness little))
      (endianness big)
      (endianness little)))

(define u8v  (make-u8vector 16384))
(define u16v (make-u16vector 8192))
(define u32v (make-u32vector 4196))
(define u64v (make-u64vector 2048))


(with-benchmark-prefix "ref/set!"

  (benchmark "bytevector-u8-ref" 1000000
    (bytevector-u8-ref bv 0))

  (benchmark "bytevector-u16-ref (foreign)" 1000000
    (bytevector-u16-ref bv 0 %foreign-endianness))

  (benchmark "bytevector-u16-ref (native)" 1000000
    (bytevector-u16-ref bv 0 %native-endianness))

  (benchmark "bytevector-u16-native-ref" 1000000
    (bytevector-u16-native-ref bv 0))

  (benchmark "bytevector-u32-ref (foreign)" 1000000
    (bytevector-u32-ref bv 0 %foreign-endianness))

  (benchmark "bytevector-u32-ref (native)" 1000000
    (bytevector-u32-ref bv 0 %native-endianness))

  (benchmark "bytevector-u32-native-ref" 1000000
    (bytevector-u32-native-ref bv 0))

  (benchmark "bytevector-u64-ref (foreign)" 1000000
    (bytevector-u64-ref bv 0 %foreign-endianness))

  (benchmark "bytevector-u64-ref (native)" 1000000
    (bytevector-u64-ref bv 0 %native-endianness))

  (benchmark "bytevector-u64-native-ref" 1000000
    (bytevector-u16-native-ref bv 0)))


(with-benchmark-prefix "lists"

  (benchmark "bytevector->u8-list" 2000
    (bytevector->u8-list bv))

  (benchmark "bytevector->uint-list 16-bit" 2000
    (bytevector->uint-list bv (native-endianness) 2))

  (benchmark "bytevector->uint-list 64-bit" 2000
    (bytevector->uint-list bv (native-endianness) 8)))


(with-benchmark-prefix "SRFI-4" ;; for comparison

  (benchmark "u8vector-ref" 1000000
    (u8vector-ref u8v 0))

  (benchmark "u16vector-ref" 1000000
    (u16vector-ref u16v 0))

  (benchmark "u32vector-ref" 1000000
    (u32vector-ref u32v 0))

  (benchmark "u64vector-ref" 1000000
    (u64vector-ref u64v 0)))
