Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the reasons for this benchmark result?

Two functions that convert a rgb image to a gray scale image:

function rgb2gray_loop{T<:FloatingPoint}(A::Array{T,3})
  r,c = size(A)
  gray = similar(A,r,c)
  for i = 1:r
    for j = 1:c
      @inbounds gray[i,j] = 0.299*A[i,j,1] + 0.587*A[i,j,2] + 0.114 *A[i,j,3]
    end
  end
  return gray
end

And:

function rgb2gray_vec{T<:FloatingPoint}(A::Array{T,3})
  gray = similar(A,size(A)[1:2]...)
  gray = 0.299*A[:,:,1] + 0.587*A[:,:,2] + 0.114 *A[:,:,3]
  return gray
end

The first one is using loops, while the second one uses vectorization.

When benchmarking them (with the Benchmark package) I get the following results for different sized input images (f1 is the loop version, f2 the vectorized version):

A = rand(50,50,3):

| Row | Function | Average     | Relative | Replications |
|-----|----------|-------------|----------|--------------|
| 1   | "f1"     | 3.23746e-5  | 1.0      | 1000         |
| 2   | "f2"     | 0.000160214 | 4.94875  | 1000         |

A = rand(500,500,3):

| Row | Function | Average    | Relative | Replications |
|-----|----------|------------|----------|--------------|
| 1   | "f1"     | 0.00783007 | 1.0      | 100          |
| 2   | "f2"     | 0.0153099  | 1.95527  | 100          |

A = rand(5000,5000,3):

| Row | Function | Average  | Relative | Replications |
|-----|----------|----------|----------|--------------|
| 1   | "f1"     | 1.60534  | 2.56553  | 10           |
| 2   | "f2"     | 0.625734 | 1.0      | 10           |

I expected one function to be faster than the other (maybe f1 because of the inbounds macro).

But I can't explain, why the vectorized version gets faster for larger images. Why is that?

like image 316
reschu Avatar asked Apr 20 '15 08:04

reschu


People also ask

What are benchmark results?

A benchmark is simply a test that helps you compare similar products. Each of our benchmarks produces a score. The higher the score, the better the performance. So instead of trying to compare devices by looking at their specifications, you can just compare the benchmark scores. It's that easy.

What is the purpose of a benchmark test?

Benchmark Testing evaluates students, grades 1-12, in math and reading, against specific grade-level standards. The testing is used to assess children on grade level to see where they are compared to other children in the grade. It can used to identify a student's strengths and weaknesses.

What is the importance of an benchmark data?

Businesses can use benchmarking in their operations to measure themselves against internal or external standards. Benchmarking can be used to measure internal progress, performance against competitors and how your processes rank against world-class organizations.


1 Answers

The answer for the results is that multidimensional arrays in Julia are stored in column-major order. See Julias Memory Order.

Fixed looped version, regarding column-major-order (inner and outer loop variables swapped):

function rgb2gray_loop{T<:FloatingPoint}(A::Array{T,3})
  r,c = size(A)
  gray = similar(A,r,c)
  for j = 1:c
    for i = 1:r
      @inbounds gray[i,j] = 0.299*A[i,j,1] + 0.587*A[i,j,2] + 0.114 *A[i,j,3]
    end
  end
  return gray
end

New results for A = rand(5000,5000,3):

| Row | Function | Average  | Relative | Replications |
|-----|----------|----------|----------|--------------|
| 1   | "f1"     | 0.107275 | 1.0      | 10           |
| 2   | "f2"     | 0.646872 | 6.03004  | 10           |

And the results for smaller Arrays:

A = rand(500,500,3):

| Row | Function | Average    | Relative | Replications |
|-----|----------|------------|----------|--------------|
| 1   | "f1"     | 0.00236405 | 1.0      | 100          |
| 2   | "f2"     | 0.0207249  | 8.76671  | 100          |

A = rand(50,50,3):

| Row | Function | Average     | Relative | Replications |
|-----|----------|-------------|----------|--------------|
| 1   | "f1"     | 4.29321e-5  | 1.0      | 1000         |
| 2   | "f2"     | 0.000224518 | 5.22961  | 1000         |
like image 76
reschu Avatar answered Sep 30 '22 13:09

reschu