Results 1 to 3 of 3

Thread: Optimal Code [introduction]

  1. #1

    Optimal Code [introduction]

    Intro
    At one time or another you've probably all had this situation where you've written yourself a procedure or function. Not a big one, just a couple, maybe 20 or 30 lines of code. And then, upon rereading it you think, 'nah, this is too big, this can be done better!'. You then think about it for a couple minutes, delete a section and replace it with a version that is three or four lines shorter. Confident that you've made a big improvement you continue. But was it really an improvement? Or better yet, weren't there better ways to optimize the code?

    Idea
    Ok, the idea is as follows. PM me with a procedure/function of your choice. Explain in a few lines what it's purpose is and why you think it can be done better or why you think the current solution is 'wrong'. If you already have a way to optimize it, please include that as well. Each week I'll then create a new thread with the suggested problem. After that, everyone can then suggest ways to optimize the code within that thread.

    I want to keep this new event as simple as possible in the beginning and take it from there. Depending on the level of success I might change or add a few 'rules' or alter the release interval of new code problems.

    In the meantime, I hope you'll find this a nice addition and I look forward to your entries.


    To get to the first problem click here.





  2. #2
    I know this is quite old but I really like this idea, I just recently picked up Delphi again to make a slot machine game and building routines for handling paylines, scrolling reels (although random and not so cool looking atm), with the litte bump at the end, feature with wild cards and free spins etc, lots of routines and sub routines.

    I have re-written whole section, parts of, or made common/specific tasked units just to house procedures/functions to do a cleaner job and keep the clutter out of the main document (more especially where I can see it being called often).

    Not just that but even now as I aim to add more, I keep thinking about how I can minimize or improve what's there and working perfectly...... lol.

  3. #3
    Sad thread, a good idea but little happened. Maybe it was too vague? It would have to be about specific tasks, small code snippets. I mean, things like checking if a line passes through a triangle, to mention one. In many cases, I think we can just look up our respective libraries of reusable code, and that could be interesting enough.

    Let me suggest one problem: Inverting a matrix. I have this, which inverts a 3x3 matrix, but only that:


    function Invert(m: Matrix3D): Matrix3D;
    var
    a,b,c,d,e,f,g,h,i,det: GLfloat;
    index: Longint;
    begin
    a := m[0];b := m[3];c := m[6];
    d := m[1];e := m[4];f := m[7];
    g := m[2];h := m[5];i := m[8];

    det := a*(e*i-f*h) - b*(d*i-f*g) + c*(d*h-e*g);

    m[0] := (e*i-f*h); m[3] :=-(b*i-c*h); m[6] := (b*f-c*e);
    m[1] := (f*g-d*i); m[4] := (a*i-c*g); m[7] := (c*d-a*f);
    m[2] := (d*h-e*g); m[5] := (b*g-a*h); m[8] := (a*e-b*d);

    for index := 0 to 8 do
    m[index] := m[index] / det;

    Invert := m;
    end;

    The obvious improvement would be a general algorithm, or at least 4x4. Can that be made simple?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •