Is what I think is gonna happen gonna happen

This is a discussion on Is what I think is gonna happen gonna happen within the C++ Programming forums, part of the General Programming Boards category; I'm trying to get rid of some inefficiencies in my code. The current (simplified) version is this: Code: class matrix ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    Is what I think is gonna happen gonna happen

    I'm trying to get rid of some inefficiencies in my code. The current (simplified) version is this:

    Code:
    class matrix {
    
    double data*; matrix& operator=(const matrix& m) {
    (for each element of m.data) { data[i] = m.data[i];}
    } matrix getminor(range_of_minor) {
    matrix m; copy_all_elements_of_this_in_range_to_m; return m;
    }
    }
    Then I would do this:
    Code:
    matrix big_matrix, smaller_matrix;
    smaller_matrix = big_matrix.getminor(range_of_minor);
    Am I copying matrices twice in the above code? Once when matrix::getminor creates a minor matrix and once when it assigns it to another one? Would it be better if I rewrote the function so that smaller_matrix is passed by reference to matrix::getminor and the values of data are copied directly?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Maybe. However, I would imagine that any smart compiler would optimize out the second construction.

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,215
    Instead of relying on the compiler to do something clever in that context, overload a constructor to create the submatrix, the "minor" of ''matrix matrix::getminor(p1, ..., pn)', directly as the elements of a new matrix.

    If you now have this:

    Code:
    matrix a;
    matrix b;
    // ...
    b = a.getminor(x, y, cx, cy);
    You'll get thins:

    Code:
    matrix a;
    // ...
    matrix b(a, x, y, cx, cy);
    (If there are any, you may want to lift the duplicate parts of 'matrix::matrix(const matrix &)', 'matrix & matrix:perator = (const matrix &)', 'matrix matrix::getminor(p1, ..., pn)', and 'void matrix::getminor(matrix &, p1, ..., pn)' out and put them in a shared function.)

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. magic could happen on recvfrom
    By -EquinoX- in forum C Programming
    Replies: 22
    Last Post: 03-27-2009, 06:06 PM
  2. I'm so freggin happy! I'm gonna have a little brother/sister!!!!!
    By Zewu in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 08-26-2003, 09:07 AM
  3. Hey, what would happen if...
    By Imperito in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 04-05-2002, 07:54 AM
  4. What would happen?
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 04-04-2002, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21