spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Complexity of enum.values()


The array returned by Colour.values() always contains the same elements, but values() creates a new array every time it's called (so it's O(n) where n is the number of enum values). And you never modify that array. So calling the method every time you need it is a waste of time. And storing an array in every instance of your DataStructure class is a waste of time and memory. I would simply call the method once and cache that array in a constant:

private static final Colour[] COLOURS =
colour.values();

public Colour getRandomColour() {
    return COLOURS[rand.nextInt(COLOURS.length)];
}

Just make sure this array is never exposed outside of the class.

Also note the usage of Random.nextInt(limit) which does exactly what you want, is probably faster, and expressed the intent more clearly than using a modulo.


Categories : Java

Related to : Complexity of enum.values()
What's the order of complexity (Big O) of this code
The inner loop is O(N^2) for prime numbers but fast for non-primes (worst case O(N^1/2) because you only have to search up to sqrt(N)). The number of prime numbers, however, is small compared to the number of non-primes. An approximation of the number of primes to up X is: X / log(X), as found in this reference link. So throwing out the non-primes as inconsequential, there are N / log(N) prime

Categories : C
Complexity of a basic algorithm?
When determining complexities, we don't include constants or coefficients. Instead of O(2N + 2), it should be O(n). We only care about numbers if they're exponential, i.e. 2^n or n^2, log2(n), etc. Putting that aside, are you sure this is O(n)? O(n) would mean that it runs n times, but it looks like j is going to catch up to n before n times. See what I'm saying? EDIT: Ok, here's what's going o

Categories : Java
SPARQL Query Computational Complexity
SPARQL itself is PSPACE-complete. You can probably only come up with the best case complexity for any given query. The real-world complexity will depend on the implementation of the database to some degree.

Categories : Algorithm
Heap Sort Space Complexity
The implementation of heapsort that you've described above sure doesn't look like it works in constant space for precisely the reason that you're worried about. However, that doesn't mean that it's not possible to implement heapsort in O(1) auxiliary space. Typically, an implementation of heapsort would reorder the elements in the array to implicitly store a binary max-heap. One nifty detail abou

Categories : Algorithm
Problems regarding calculation of time complexity of a code
first, you have convince yourself, the problem is bounded by n and the growth of s. let see how fast s grows. every iteration, the current value of i will be added and i itself will add 1. that is, in the j-th iteration, i==j. so, given any iteration, the current value of s is summing up from 1 to current i, which is roughly i^2, which will be compared to n. therefore, the number of iterations,

Categories : C
Recently Add
Redirecting the output directory of 'mvn package' or 'mvn compile' command
No suitable constructor found for ProductoExtranjero
java Composite design pattern(Directory &File)
Java JTree's ui refresh after removing node from parent
First REST Spring application
How to cancel Indexing of a Solr document using Update Request Processor
PowerMock - Mock a Singleton with a Private Constructor
Calling a Postgres stored function SQL error
Where to store Morphlines Java custom command class?
Generic repository using map
How can I scroll a ScrolledComposited in Eclipse SWT Design view?
2 Frames/layout in 1 Activity
Writing a switch differently
Next button opens another activity when its reaches the array limit
Is EclipseLink MOXy capable of applying JSR-303 Bean Validation when unmarshalling XML to object?
Why my jdk can't work,and before the java_home, there is a space that is not from me
How to add List of objects in a Map
How to make notepad++ function like regular notepad in cmd?
Cell renderer and the lost focus
how can I implement iterable for LinkedList>
Disable Androids image-crunch in eclipse (run as) builds
java 8 lambda myMap.stream().count() != myMap.size() after merging myMap
Issue with Calendar calculation that spans 2 calendar years
JSF 2.0 Spring bean injection
Java Regex ReplaceAll with grouping
Getting any word and last word using sed
Clicking on link on JEditorPane throws IOException
printing out difference of two arrays
Spring Bean Alias in JavaConfig
Using Factory Method to Create Generics
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.