How to create efficient code through unit testing?

I participate in Dojo TDD coding, where we try to practice pure TDD in simple problems. However, it seemed to me that the code that comes out of unit tests is not the most efficient. Now that’s fine, but what if the use of the code grows, so efficiency becomes a problem.

I like the way code comes from unit testing, but is it possible to get the performance property through additional tests?

Here is a trivial example in ruby: simple factorization. I followed the clean TDD approach so that the tests pass one by one, checking my initial acceptance test (commented below). What further steps could I take if I wanted to create one of the common simple factorization algorithms ? To reduce the problem area, say, I want to get a quadratic sieve . Now, in this exact case, I know the "optimal algorithm, but in most cases the client will simply add the requirement that the function work in less than" x "for the given environment.

require 'shoulda'
require 'lib/prime'

class MathTest < Test::Unit::TestCase
  context "The math module" do
    should "have a method to get primes" do 
      assert Math.respond_to? 'primes'
    end
  end
  context "The primes method of Math" do
    should "return [] for 0" do
      assert_equal [], Math.primes(0)
    end
    should "return [1] for 1 " do
      assert_equal [1], Math.primes(1)
    end
    should "return [1,2] for 2" do 
      assert_equal [1,2], Math.primes(2)
    end
    should "return [1,3] for 3" do 
      assert_equal [1,3], Math.primes(3)
    end
    should "return [1,2] for 4" do 
      assert_equal [1,2,2], Math.primes(4)
    end 
    should "return [1,5] for 5" do 
      assert_equal [1,5], Math.primes(5)
    end   
    should "return [1,2,3] for 6" do 
      assert_equal [1,2,3], Math.primes(6)
    end       
    should "return [1,3] for 9" do 
      assert_equal [1,3,3], Math.primes(9)
    end        
    should "return [1,2,5] for 10" do 
      assert_equal [1,2,5], Math.primes(10)
    end                  
  end
#  context "Functionnal Acceptance test 1" do
#    context "the prime factors of 14101980 are 1,2,2,3,5,61,3853"do      
#      should "return  [1,2,3,5,61,3853] for ${14101980*14101980}" do
#        assert_equal [1,2,2,3,5,61,3853], Math.primes(14101980*14101980)
#      end
#    end
#  end
end

and naive algorithm I created by this approach

module Math
  def self.primes(n)
    if n==0
      return []
    else
      primes=[1]  
      for i in 2..n do
        if n%i==0          
          while(n%i==0)
            primes<<i
            n=n/i
          end
        end
      end      
      primes
    end
  end
end

edit 1 , , : unit test, , .

edit 2 , , , . , emerge, : ? , : , , .

+3
7
  • TDD
  • , .

TDD- Dojo, , , ( , , ) - .

TDD Dojo

  • , ( , ).
  • ,
  • ( )

, , , . , ( : -, , , , , ).

, TDD , TDD . , , ( ).

, TDD ( , ). , , . , , . , . TDD, .

, - .

+4

, . - , - - .

+3

TDD - . , TDD (: , , zillion frameworks/libraries).

TDD - , ? , ? . ; , , .

. TDD , , , quicksort, . .

: . http://www.markhneedham.com/blog/2009/12/10/tdd-big-leaps-and-small-steps/ , reddit. , . .

+3

.

, , unit test, (, O (N ln N).

, unit test, , , , , . (, , , , .

, . , , , , , .

+2

unit test , . :

start_time = date_time_now();
Math.primes(1000);
stop_time = date_time_now();
assert stop_time-start_time < target_execution_time;

, . .

, elapsed_time - . cpu_time, , / ..

+1

. : " " x " ". , . , , , , , .

, BlueRaja , , .

0

, , , TDD, , , .

. , , . , .

I think you can write one that will work reliably on any platform by setting a basic level. You will need some kind of infrastructure that will help you, but can it look like this:

TEST: should be faster than O(n^2)
setup: baseline_time_for_10 = time_of( f(10) )
100: assert time_of(f(100)) < baseline_time_for_10 ^ 2    
etc.

I always wanted to do this, but I did not have a suitable opportunity for the project. Let us know how this happens.

0
source

Source: https://habr.com/ru/post/1740833/


All Articles