Page 2 of 2 FirstFirst 12
Results 21 to 28 of 28
  1. #21
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Actually that's the tip me and fubarable talking about. Same thing can be done in two different way. He's try to workout using split() and I'm working on with it using regex.

  2. #22
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    yup...
    well, they both use regex. String split is just a wrapper.
    Java Code:
    [COLOR=GREEN]/* from src: java.lang.String.java */[/COLOR]
    
    [COLOR=GREEN][I]// OVERLOADED METHOD[/I][/COLOR]
    [COLOR=PURPLE]public[/COLOR] String[COLOR=BLACK][[/COLOR][COLOR=BLACK]][/COLOR] split[COLOR=BLACK]([/COLOR]String regex[COLOR=BLACK])[/COLOR] [COLOR=BLACK]{[/COLOR]
      [COLOR=NAVY]return[/COLOR] split[COLOR=BLACK]([/COLOR]regex[COLOR=BLACK],[/COLOR] [COLOR=ORANGE][B]0[/B][/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
    [COLOR=BLACK]}[/COLOR]
    
    [COLOR=GREEN][I]// OVERLOADED METHOD[/I][/COLOR]
    [COLOR=PURPLE]public[/COLOR] String[COLOR=BLACK][[/COLOR][COLOR=BLACK]][/COLOR] split[COLOR=BLACK]([/COLOR]String regex[COLOR=BLACK],[/COLOR] [COLOR=PURPLE]int[/COLOR] limit[COLOR=BLACK])[/COLOR] [COLOR=BLACK]{[/COLOR]
      [COLOR=NAVY]return[/COLOR] Pattern[COLOR=BLACK].[/COLOR]compile[COLOR=BLACK]([/COLOR]regex[COLOR=BLACK])[/COLOR][COLOR=BLACK].[/COLOR]split[COLOR=BLACK]([/COLOR][COLOR=NAVY]this[/COLOR][COLOR=BLACK],[/COLOR] limit[COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
    [COLOR=BLACK]}[/COLOR]
    Since they're almost the same, i thought the performace would be similar. But on 10,000,000 loops (discarding results) eranga's code turned out to be 21 secs faster.
    fubarable's solution -- 0:03:09.182
    eranga's solution ---- 0:02:48.512

    Java Code:
      [COLOR=PURPLE]private[/COLOR] [COLOR=PURPLE]final[/COLOR] [COLOR=PURPLE]static[/COLOR] String s [COLOR=BLACK]=[/COLOR] 
        [COLOR=SILVER]"abc/123/abc/123/abc/123/abc/123/abc/123/abc/123/abc/123"[/COLOR][COLOR=BLACK];[/COLOR]
      [COLOR=PURPLE]private[/COLOR] [COLOR=PURPLE]final[/COLOR] [COLOR=PURPLE]static[/COLOR] [COLOR=PURPLE]int[/COLOR] MAX [COLOR=BLACK]=[/COLOR] [COLOR=ORANGE][B]10000000[/B][/COLOR][COLOR=BLACK];[/COLOR] [COLOR=GREEN][I]// 10,000,000[/I][/COLOR]
      
      [COLOR=GREEN]/* discards results */[/COLOR]
      [COLOR=PURPLE]public[/COLOR] [COLOR=PURPLE]static[/COLOR] [COLOR=PURPLE]void[/COLOR] fubarable[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK]{[/COLOR]
        [COLOR=PURPLE]int[/COLOR] i [COLOR=BLACK]=[/COLOR] [COLOR=ORANGE][B]0[/B][/COLOR][COLOR=BLACK];[/COLOR]
        [COLOR=NAVY]while[/COLOR] [COLOR=BLACK]([/COLOR]i[COLOR=BLACK]++[/COLOR] [COLOR=BLACK]<[/COLOR] MAX[COLOR=BLACK])[/COLOR]
        [COLOR=BLACK]{[/COLOR]
          s[COLOR=BLACK].[/COLOR]split[COLOR=BLACK]([/COLOR][COLOR=SILVER]"/"[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        [COLOR=BLACK]}[/COLOR]
      [COLOR=BLACK]}[/COLOR]
      
      [COLOR=GREEN]/* discards results */[/COLOR]
      [COLOR=PURPLE]public[/COLOR] [COLOR=PURPLE]static[/COLOR] [COLOR=PURPLE]void[/COLOR] eranga[COLOR=BLACK]([/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK]{[/COLOR]
        Pattern p [COLOR=BLACK]=[/COLOR] Pattern[COLOR=BLACK].[/COLOR]compile[COLOR=BLACK]([/COLOR][COLOR=SILVER]"[/s]"[/COLOR][COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        [COLOR=PURPLE]int[/COLOR] i [COLOR=BLACK]=[/COLOR] [COLOR=ORANGE][B]0[/B][/COLOR][COLOR=BLACK];[/COLOR]
        [COLOR=NAVY]while[/COLOR] [COLOR=BLACK]([/COLOR]i[COLOR=BLACK]++[/COLOR] [COLOR=BLACK]<[/COLOR] MAX[COLOR=BLACK])[/COLOR]
        [COLOR=BLACK]{[/COLOR]
          p[COLOR=BLACK].[/COLOR]split[COLOR=BLACK]([/COLOR]s[COLOR=BLACK])[/COLOR][COLOR=BLACK];[/COLOR]
        [COLOR=BLACK]}[/COLOR]
      [COLOR=BLACK]}[/COLOR]
    *Let me know if you don't think this is a fair comparision.
    Last edited by angryboy; 03-13-2009 at 12:19 AM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #23
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    That's why all the time I suggest RegEx for this kind of evaluations. But as I said, challenge is build the proper regular expression, lots of people still not aware about RegEx.

  4. #24
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    /* discards results */
    This line of code is quite significant. Suppose the actual computation took 1/100-th of a second. (The discarded part, I mean. Including the scanner doing its scanning.)

    Then the saving would then be 21 seconds out of a day and a bit.
    Last edited by pbrockway2; 03-13-2009 at 11:42 PM.

  5. #25
    Sticks_ll is offline Member
    Join Date
    Jun 2008
    Posts
    8
    Rep Power
    0

    Default

    thankyou for your help. I think I have it sorted now. As for this RegeX thing...interesting, but I feel I would be showing off if I incorporated that, and doubt my teacher even know about it :P
    "lots of people still not aware about RegEx. "
    anywho can I ask what books you guys learnt from?

  6. #26
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Use of RegEx in your application make it more powerful. I'm really worried about that if you teacher don't know about RegEx. ;)

    Read whatever you found, may be on a book or on the web, or may be in class handbook/discussion. That's my secret. And working experience too.

  7. #27
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    anywho can I ask what books you guys learnt from?
    any book on jdk1.4 and after should have a chapter (or section) on regex.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  8. #28
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Page 2 of 2 FirstFirst 12

Similar Threads

  1. ECG plot in a textfile
    By samson in forum Java 2D
    Replies: 13
    Last Post: 01-28-2012, 04:02 PM
  2. Inserting a Table in a TextFile?
    By Keerti in forum Advanced Java
    Replies: 5
    Last Post: 11-19-2008, 04:31 PM
  3. Search TextFile
    By gsupriyarao@yahoo.com in forum Advanced Java
    Replies: 5
    Last Post: 04-11-2008, 11:03 AM
  4. textfile with Java problems
    By saytri in forum New To Java
    Replies: 4
    Last Post: 12-29-2007, 05:16 PM
  5. Textfile and GUI problems
    By saytri in forum New To Java
    Replies: 2
    Last Post: 12-21-2007, 04:08 PM

Posting Permissions

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