Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize JavaStream #54

Open
akiradeveloper opened this issue Jan 23, 2023 · 0 comments
Open

Optimize JavaStream #54

akiradeveloper opened this issue Jan 23, 2023 · 0 comments

Comments

@akiradeveloper
Copy link
Owner

Using a scanner like this will actually improve the performance (All bench completes about 100ms) but it is not a normal way. Can we do the same thing with the standard library?

class FastScanner \{
    BufferedReader br;
    StringTokenizer st;

    public FastScanner(String s) \{
        try \{
            br = new BufferedReader(new FileReader(s));
        } catch (FileNotFoundException e) \{
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public FastScanner() \{
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String nextToken() \{
        while (st == null || !st.hasMoreElements()) \{
            try \{
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) \{
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    String next() \{
        return nextToken();
    }

    int nextInt() \{
        return Integer.parseInt(nextToken());
    }

    long nextLong() \{
        return Long.parseLong(nextToken());
    }

    double nextDouble() \{
        return Double.parseDouble(nextToken());
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant