hate these ads?, log in or register to hide them
Page 2 of 8 FirstFirst 12345 ... LastLast
Results 21 to 40 of 152

Thread: Dumb Programming Questions - Pro Programming Answers Megathread!

  1. #21
    Donor
    Join Date
    April 9, 2011
    Posts
    1,290
    Quote Originally Posted by Waagaa View Post
    Your run method should have a loop somewhere, now your server only parses one message and then the thread stops. Do you see "** Server thread finished" after the first message is handled?
    Now the socket is probably kept open when you exit the run() method, which means that the next time the thread starts up it cannot connect to the socket because it's already claimed on OS level by the now dead thread.
    It's better design to have the while loop inside the run method of your Thread and just let the main method spawn the thread.
    Then in the main method make something that monitors key input and when you hit ENTER orso it tells the thread to die.
    If you need more help, let me know and I'll skeleton it up for you.
    Got it working now, thanks. I knew I needed a while loop in the client but hadn't thought I needed one in the server.

  2. #22
    Donor Sponk's Avatar
    Join Date
    April 10, 2011
    Location
    AU TZ
    Posts
    8,026
    a few things:

    1. Nobody extends from Thread any more. They implement Runnable instead.
    2. validateInput("") will throw an exception.
    3. readHostAndPort doesn't need nested tries, so make them flatter, e.g. try {x} catch (Yy) {;} catch (Z z) {;}
    4. readHostAndPort() probably should have a finally {if (in !=null) in.close();} block
    5. the server-side code should also tidy itself up if an exception is thrown. You can test it using Mockito or something.
    Contract stuff to Seraphina Amaranth.

    "You give me the awful impression - I hate to have to say - of someone who hasn't read any of the arguments against your position. Ever."

  3. #23
    Donor
    Join Date
    April 9, 2011
    Posts
    1,290
    Quote Originally Posted by Sponk View Post
    a few things:

    1. Nobody extends from Thread any more. They implement Runnable instead.
    2. validateInput("") will throw an exception.
    3. readHostAndPort doesn't need nested tries, so make them flatter, e.g. try {x} catch (Yy) {;} catch (Z z) {;}
    4. readHostAndPort() probably should have a finally {if (in !=null) in.close();} block
    5. the server-side code should also tidy itself up if an exception is thrown. You can test it using Mockito or something.
    I pretty much ripped off the code that the lecturer provided a few weeks ago and worked with that. I'll look into making some of these changes as I'm not terrible at Java but no where near good enough to start something like this from scratch.

  4. #24
    Donor
    Join Date
    April 11, 2011
    Location
    The Netherlands
    Posts
    694
    Quote Originally Posted by Sponk View Post
    a few things:

    1. Nobody extends from Thread any more. They implement Runnable instead.
    2. validateInput("") will throw an exception.
    3. readHostAndPort doesn't need nested tries, so make them flatter, e.g. try {x} catch (Yy) {;} catch (Z z) {;}
    4. readHostAndPort() probably should have a finally {if (in !=null) in.close();} block
    5. the server-side code should also tidy itself up if an exception is thrown. You can test it using Mockito or something.
    1. Nah, I have to extend Thread quite a bit, implementing Runnable doesn't allow you to do the more advance threading stuff. Still, he'd be fine just implementing Runnable, aye.

    You probably want to free up the OS resources like ports btw... close() those streams/sockets/etc. Do that in a finally block, so stuff gets called even when you get an exception. Or if you're under Java 7, use the using() clause.

    apache.commons.ioutils give you a nice closeQuietly() that you can use.

  5. #25

    Join Date
    April 16, 2011
    Posts
    128
    I need a bit of help.
    I have a python script that fetches data from a database and makes graphs. It's a fair amount of data for each graph and there's a couple of hundred graphs. Now my program makes some nice graphs, but the speed is somewhat lacking. As I mentioned it's written in Python and I'm using matplotlib for the plotting.

    Do any of you have a few suggestions to how I could either speed it up, use something other than matplotlib or how to do it in other languages? Other languages would be primarily Java (or C but that would be too much work I'm sure).

  6. #26

    Join Date
    September 13, 2011
    Location
    Norway
    Posts
    587
    Define speed and how you implented it ...

  7. #27
    Donor Sponk's Avatar
    Join Date
    April 10, 2011
    Location
    AU TZ
    Posts
    8,026
    More to the point, what have you done to investigate where the bottleneck is?

    I mean, if you're selecting a million rows from the database one row at a time without reusing the db connection, it doesn't matter how fast your computation is.

    Conversely, if you're doing something stupidly O(n^2) then you should look at that first.
    Contract stuff to Seraphina Amaranth.

    "You give me the awful impression - I hate to have to say - of someone who hasn't read any of the arguments against your position. Ever."

  8. #28
    Donor
    Join Date
    April 9, 2011
    Posts
    1,123
    Generally speaking, Python wouldn't be the bottleneck in this kind of situation. It's probably the database, or more specifically, the queries.
    meh

  9. #29

    Join Date
    April 16, 2011
    Posts
    128
    As far as my timing experiment go about 3/5 of the time is used getting data and the other 2/5 is used in matplotlib routines. I'm not too worried about the time of getting the data as I'll be moving the script onto the same server as the database soon, so that should speed that part of it up nicely.

    Any O(n^2) has been weeded out a while ago.

    Right now I fire off separate threads to get the data for each graph and once all the data has been collected I make all the graphs in one thread as matplotlib isn't thread safe.

  10. #30
    balistic void's Avatar
    Join Date
    May 5, 2011
    Location
    Dublin/London
    Posts
    1,836
    Dunno about the rest of your bottlenecks but I can comment on the graphics... I write high performance charting software. All the free/commercial charting libraries available are pretty slow, as they target the lowest common denominator hardware and do everything in software. I do the graphics myself with OpenGL/DirectX. Can happily chart millions of datapoints whilst being able to smoothly scroll and zoom etc. Even took it a step further and wrapped it up so it can be integrated into a c# wpf application easily. Also did some opengl prototypes for android etc.

    This is clearly an awful lot of work, but if you are interested I would suggest giving it a go. It's a good way to learn how to program graphics. The basic trick is everything must be converted to triangles, so get out pen and paper and draw diagram: given 2 points (x1,y1), (x2,y2) -> it is possible to generated a strip of 3 triangles that form the "shaded section" under these points going down to the axis. Drawing the chart line itself is easy, but everyone expects fancy shaded section. If using older graphics software, you gotta generate this geometry with cpu and then push to graphics card. If using newer stuff then use Geometry shader to "emit" the triangles from the input data.

    Drawing graph is pretty easy, drawing the ruling and text labels is actually where the difficulty lies
    Old screenshot: http://dl.dropbox.com/u/4436079/imag...nshot-ARES.png (axis fail) Note how it's antialiased and stuff. This is great for screenshots, but causes shimmering artifacts when it scrolls >.<
    Last edited by balistic void; April 20 2012 at 08:36:03 AM.

  11. #31
    shoki's Avatar
    Join Date
    April 9, 2011
    Location
    Croatia
    Posts
    864
    Quote Originally Posted by erichkknaar View Post
    Generally speaking, Python wouldn't be the bottleneck in this kind of situation. It's probably the database, or more specifically, the queries.
    Quote Originally Posted by Renox View Post
    As far as my timing experiment go about 3/5 of the time is used getting data and the other 2/5 is used in matplotlib routines. safe.
    it definitely sounds like a DB issue, not graphics.
    Do you have indexes all sorted out? Some kind of procedure on the DB to mine for data?
    ingame: AntonioBanderas
    Detecting epic potential, expecting epic fail.
    Ah yes, the fork: The poor man's trident

  12. #32
    bitches love my mad sed skills Donor tekai's Avatar
    Join Date
    April 9, 2011
    Location
    Hampurissa
    Posts
    1,017
    New question:
    In ASM with more syntax & macros aka C: How do you deal with a set of cars and a set of elephants w/o writing a 2nd set of functions to return elephants?
    C++ has templates, Java something similar, other languages just don't care, but how do I do it in C?

    The Chippewa believed that the weasel could kill the dreaded wendigo giant by rushing up its anus.

  13. #33
    balistic void's Avatar
    Join Date
    May 5, 2011
    Location
    Dublin/London
    Posts
    1,836
    Function pointers.

  14. #34
    shoki's Avatar
    Join Date
    April 9, 2011
    Location
    Croatia
    Posts
    864
    Quote Originally Posted by tekai View Post
    New question:
    In ASM with more syntax & macros aka C: How do you deal with a set of cars and a set of elephants w/o writing a 2nd set of functions to return elephants?
    C++ has templates, Java something similar, other languages just don't care, but how do I do it in C?
    Quote Originally Posted by balistic void View Post
    Function pointers.
    ^^what he said
    void pointers for data, function pointers for actually doing stuff with data
    ingame: AntonioBanderas
    Detecting epic potential, expecting epic fail.
    Ah yes, the fork: The poor man's trident

  15. #35

    Join Date
    April 9, 2011
    Posts
    48
    Quote Originally Posted by Renox View Post
    Right now I fire off separate threads to get the data for each graph and once all the data has been collected I make all the graphs in one thread as matplotlib isn't thread safe.
    I hope u arent using threads. Use the python multiprocessing package. Threads in python are terribly slow.

  16. #36

    Join Date
    September 13, 2011
    Location
    Norway
    Posts
    587
    Is there a list of what values one can pass to Windows API's? Like how do I know that MessageBox from user32.dll requires 4 values?

    Code:
    [DllImport("user32.dll", CharSet=CharSet.Auto)]
    static extern int MessageBox(int hWnd, string text, string caption, int options);
    MessageBox(0 , "Text", "Caption", 0);

  17. #37
    balistic void's Avatar
    Join Date
    May 5, 2011
    Location
    Dublin/London
    Posts
    1,836

  18. #38

    Join Date
    September 13, 2011
    Location
    Norway
    Posts
    587
    MessageBox was just an example. But the link you gave me seem to be what I'm looking for. Thanks.

  19. #39
    balistic void's Avatar
    Join Date
    May 5, 2011
    Location
    Dublin/London
    Posts
    1,836
    If using VS you can just press F1 with cursor on the function you want, should open msdn.

  20. #40
    Donor Aea's Avatar
    Join Date
    April 13, 2011
    Location
    Austin
    Posts
    1,950
    Quote Originally Posted by balistic void View Post
    Surprising how many starting / junior developers don't know this. Between the official documentation, stack overflow, and google I am able to figure out 99% of the issues I run into. The remaining require some experimentation of library code lookup. A major difference between senior and mid-level programers boils down to google-fu.

Bookmarks

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
  •