-7

I have this code and when I try to compile it, it returns:

E:\temp\JavaApplication12\src\javaapplication12\JavaApplication12.java:15: error: code too large
    public static void main(String[] args) {
1 error

My code is a sudoku solver. First I need to load all the numbers, and then process which numbers that are already present on rows and columns, to decide what I can solve. But it doesn't compile the code! I have spent weeks working on this.

The approach of my sudoku solver solve the problem in constant time. So, I am not using cycles or arrays because it will make the problem O(n). I want O(k) where k is the constant.

sinsuren
  • 1,745
  • 2
  • 23
  • 26
youyou
  • 1
  • 1
  • 2
  • 8
    Because... your code is.... too large. Seriously, a 40 thousand line class? – Andy Turner Aug 26 '16 at 12:35
  • 3
    may be this will help http://stackoverflow.com/questions/2407912/code-too-large-compilation-error-in-java – Bibek Shakya Aug 26 '16 at 12:36
  • @AndyTurner - I do know arrays, but I do not use it, because it is slower. My goal is performance, thats why dont use loops and arrays. I want to solve it in CONSTANT time. – youyou Aug 26 '16 at 12:37
  • 2
    There is literally nothing slower than code which can't be compiled. Training ants to carry grains of rice representing the bits in the calculation would be faster, including training time. – Andy Turner Aug 26 '16 at 12:37
  • 5
    "*I do know arrays, but I do not use it, because it is slower. My goal is performance, thats why dont use loops and arrays*" - no offense, that's simply stupid. –  Aug 26 '16 at 12:38
  • @AndyTurner - try to find Sudoku solver that runs in constant time, there is none, but I would have constant code with constant time. – youyou Aug 26 '16 at 12:38
  • 2
    There was so much code that my browser tab crashed, that's quite something. – Timo Salomäki Aug 26 '16 at 12:38
  • 2
    @youyou nope, you've got 40k lines of text. This doesn't run in constant time, it doesn't run at all. – Andy Turner Aug 26 '16 at 12:39
  • @a_horse_with_no_name - just tell me different approach to achieve constant-time sudoku solver.... – youyou Aug 26 '16 at 12:39
  • 2
    Seems like the number of operations is roughly equivalent anyway: have you proven your... code is faster? That this monstrosity is the best optimization? I'm skeptical. – Dave Newton Aug 26 '16 at 12:40
  • @DaveNewton - I cant prove it, because I cant run it :( – youyou Aug 26 '16 at 12:42
  • 2
    I might focus on some analysis first. There are optimizations besides unrolling entire loops - but the chances of you optimizing Java code better than the JIT are low. – Dave Newton Aug 26 '16 at 12:44
  • 2
    **What** you are trying to do or optimize is irrelevant. You are not allowed to have a code of that length. _Period_! There cannot be an answer to this question. It should be closed as unproductive. – Fildor Aug 26 '16 at 12:51
  • 2
    @youyou I think you are under the mistaken impression that a for loop makes something linear in time: `for (int i = 0; i < 9; ++i) { ... }` runs in constant time. – Andy Turner Aug 26 '16 at 12:51
  • 2
    "Constant time" is not automatically fast, and "linear" is definitely not always slow. In any case, an algorithm with fixed-sized input (like your 9x9 Sudoku solver) can't properly be described with big-O notation anyway. You'd have to write something like an NxN sudoku solver before you could call it O(anything) –  Aug 26 '16 at 13:02
  • 40k lines of code! Just curious, how much time it took for you to write it? – SkrewEverything Aug 26 '16 at 13:33
  • @H-H the code was generated. It is far too uniform to have been written by hand. – Andy Turner Aug 26 '16 at 15:09
  • Generated. OK, that's the only good point. So throw the generated code away and turn the generator into a solver. Doing loop unrolling manually is completely pointless as 1. Java is damn good at it, 2. the lengthy code prevents other optimizations. Moreover, it's a stupid idea. You might gain 20% with low-level optimizations if you're good at them. You may get orders of magnitude with a smart algorithm. – maaartinus Aug 26 '16 at 21:53
  • FYI, even if your code compiled to a .class file, HotSpot won't compile large methods from bytecode to native code, if I remember correctly, so it will run them in the interpreter instead, which will be *so much* slower than if you used loops. – Boann Aug 27 '16 at 19:12

1 Answers1

1

Even if the code compiled, it wouldn't solve a game of Sudoku. Actually, all it does is to set the 9 variables bN to true if any of the 81 variables aPQ are equal to N.

And it doesn't even do this efficiently. There are 1458 (=18*81) conditions setting each of the bN variables to true. (Simple check: each of the conditions is 3 lines; 1458 checks for each of 9 variables: 3 * 1458 * 9 = 39366, the approximate length of the file).

All of the setters of bN are independent, and are idempotent, so they can be arbitrarily rearranged and the 17 repeated checks of the conditions can be removed.

An equivalent (and adequately efficient) version of this code - using arrays - is:

// Using 10 as array size, as OP's code is one-based;
// first element is unused.
int a[][] = new int[10][10];
// Initialize the elements of a.
boolean b[] = new boolean[10];
for (int i = 1; i <= 9; i++) {
  for (int j = 1; j <= 9; j++) {
    if (a[i][j] >= 1 && a[i][j] <= 9) {
      b[a[i][j]] = true;
    }
  }
}

which should fit inside the maximum size of a method quite easily.

You should focus on writing correct, maintainable code, before considering how to make it efficient - this code doesn't work for its stated purpose, and I would not want to be the one working out where the bug in 40k lines of code is. The only reason I was able to analyse this much code is that it appears to be generated, as it is very uniform in its pattern.


I did the analysis above using a (very hacky) Python script.

Run using:

curl http://pastebin.com/raw/NbyTTAdX | python script.py

script.py:

import sys
import re

with open('/dev/stdin') as fh:
  lines = fh.readlines()

bequals = re.compile(r'^b\d\s*= true;$')

i = 0

bvariablesetters = {}

while i < len(lines):
  if lines[i].strip().startswith('if (') and lines[i].strip().endswith('{'):
    # Match the conditionals setting one of the b variables.
    if lines[i+2].strip() == '}' and bequals.search(lines[i+1].strip()):
      newline = ' '.join(map(str.strip, lines[i:i+3]))

      spl = newline.split()

      # This is the "b=" variable
      bvar = spl[5]
      bvariablesetters.setdefault(bvar, []).append(' '.join(newline))
      i += 3
      continue
  else:
    # Print out lines which don't match the conditional-set-b pattern, so you
    # can see that there's nothing else going on.
    sys.stdout.write(lines[i])
    i += 1

# Print the number of conditionals setting each of the b variables.
print {(k, len(v)) for k, v in bvariablesetters.iteritems()}
# Print the number of unique conditionals setting each of the b variables.
print {(k, len(set(v))) for k, v in bvariablesetters.iteritems()}

# Print one of the lists of conditions to set a b variable.
print bvariablesetters['b1=']

# Print one of the sets of conditions to set a b variable.
print sorted(set(bvariablesetters['b1=']))
Andy Turner
  • 137,514
  • 11
  • 162
  • 243