3

well I know it is very novice question, but nothing is getting into my mind. Currently I am trying this, but it is the least efficient way for such a big number. Help me anyone.

int count = 66000000;
LinkedList<Integer> list = new LinkedList<Integer>();
        for (int i=1;i<=count;i++){
            list.add(i);
            //System.out.println(i);
        }

EDIT:

Actually I have o perform operation on whole list(queue) repeatedly (say on a condition remove some elements and add again), so having to iterate whole list became so slow what with such number it took more than 10min.

Ankit
  • 6,554
  • 6
  • 49
  • 71
  • 2
    You could write it in C. :-) – Lee Daniel Crocker Jun 10 '13 at 19:17
  • See also http://stackoverflow.com/questions/16711147/populating-a-list-with-a-contiguous-range-of-integers – Andy Thomas Jun 10 '13 at 19:18
  • Depending on the use-case you could also use an [IntRange](http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/math/IntRange.html) or implement something yourself similar based on `Set` or `List`. – jCoder Jun 10 '13 at 19:21
  • @jCoder usecase is: Josephus problem. So I need a queue. – Ankit Jun 10 '13 at 19:28
  • An ArrayList or TIntArrayList or `int[]` will be faster but usually the fastest way to do something is to not do it. I suspect with more context you might see how to eliminate the need for the list in the first place. – Peter Lawrey Jun 10 '13 at 20:44
  • @ay89, if all you need is a list of integers, you may want to look at optimizing your use-case instead. I would hazard to guess that you don't actually need to generate all these numbers, and would benefit from optimizing a different portion of your program. See if you can amortize the creation of this list of integers, or if you can use a different approach for your implementation to do away with it entirely. – kurtzbot Jun 10 '13 at 23:39

3 Answers3

8

the size of your output is O(n) therefore it's literally impossible to have an algorithm that populates your list any more efficient than O(n) time complexity.

You're spending a whole lot more time just printing your numbers to the screen than you actually are spending generating the list. If you really want to speed this code up, remove the

System.out.println(i);

On a separate note, I've noticed that you're using a LinkedList, If you used an array(or array-based list) it should be faster.

  • oh quite obvious i couldn't figure out :( – Ankit Jun 10 '13 at 19:17
  • 2
    O() notation only measures complexity, not actual performance. The unspecified constant matters, and often matters more than inherent complexity. Probably using an array instead of a list would bring that time down considerably. Some loop unrolling might help too. – Lee Daniel Crocker Jun 10 '13 at 19:19
  • yes, actually my whole operation became time consuming because of such a large number, i thought it stuck, so just to figure out I added print. – Ankit Jun 10 '13 at 19:19
  • 1
    On the other hand, if all the OP needs is a List, rather than specifically an LinkedList, the representation could be created in O(1). – Andy Thomas Jun 10 '13 at 19:24
  • @sam my usecase is best suited for a queue. so having an array might make the solution complex. – Ankit Jun 10 '13 at 19:26
  • @ay89 It's possible to make an array-based queue with some circular-buffer implementation, and I'd be surprised if Java didn't have a built-in queue of some sort – Sam I am says Reinstate Monica Jun 10 '13 at 19:28
  • 1
    pretty sure `LinkedList#add(obj)` has O(1) complexity as it's actually implementing a doubly-linked list. – DannyMo Jun 10 '13 at 19:29
  • @damo I did address that in the post – Sam I am says Reinstate Monica Jun 10 '13 at 19:33
  • Guess I misunderstood...I read it as if you were saying LinkedList.add has O(n) complexity and that the OP could optimize insertions by using something else. – DannyMo Jun 10 '13 at 19:37
  • That's the way it reads to me too. The fact that there are O(N) insertions has nothing to do with LinkedLists, and LinkedLists do appends in constant time already. Your point remains obscure. – user207421 Jun 10 '13 at 23:06
3

You could implement a List where the get(int index) method simply returns the index (or some value based on the index). The creation of the list would then be constant time (O(1)). The list would have to be immutable.

Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
0

Your question isn't just about building the list, it includes deletion and re-insertion. I suspect you should be using a HashSet, maybe even a BitSet instead of a List of any kind.

user207421
  • 305,947
  • 44
  • 307
  • 483