memory-efficient

Java Array Efficiency

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 01:06:25
I am not 100% sure of the mechanism in action so I decided to post here for further clarifications. I am doing a project that should handle large amounts of data in Java (it has to be Java). I would like it to be as efficient as possible. By efficient I mean that memory and speed calculations should come in first and readability should come in second. Now I have two ways to store my data: create one array of MyObject 1) MyObject[][] V = new MyObject[m][n] Or create two arrays of int: 2) int[][] V = new int[m][n] 3) int[][] P = new int[m][n] Clearly MyObject contains at least two fields and

Building an EFFICIENT Sudoku Solver

廉价感情. 提交于 2019-12-06 00:14:09
问题 Yes, I know this is nothing new and there are many questions already out there (it even has its own tag), but I'd like to create a Sudoku Solver in Java solely for the purpose of training myself to write code that is more efficient. Probably the easiest way to do this in a program is have a ton of for loops parse through each column and row, collect the possible values of each cell, then weed out the cells with only one possibility (whether they contain only 1 number, or they're the only cell

Python: Memory cost of importing a module

断了今生、忘了曾经 提交于 2019-12-05 17:42:10
The memory cost obviously depends on exactly how large a module is, but I'm only looking for a general answer: Is it generally expensive or cheap to import a module in Python? If I have a few tens of small scripts that potentially stay in memory for the whole duration of the application, how much will that hog the memory? It sounds like you aren't worried about time cost (good; that would be silly, since modules are only imported once) but memory cost. I put it to you: if you need all the functionality in these modules, then how exactly do you plan to avoid having them all in memory? Might as

The most efficient way to delete millions of files based on modified date, in windows

本秂侑毒 提交于 2019-12-05 13:47:29
Goal: Use a script to run through 5 million - 10 million XML files and evaluate their date, if older than 90 days delete the file. The script would be run daily. Problem: Using powershell Get-ChildItem -recurse, causes the script to lock up and fail to delete any files, I assume this is because of the way Get-ChildItem needs to build the whole array before taking any action on any file. Solution ?: After lots of research I found that [System.IO.Directory]::EnumerateFiles will be able to take action on items in the array before the array is completely built so that should make things more

How to efficiently store small byte arrays in Java?

﹥>﹥吖頭↗ 提交于 2019-12-05 12:25:04
问题 By small byte arrays I mean arrays of bytes with length from 10 up to 30. By store I mean storing them in the RAM , not serializing and persisting to the filesystem. System macOS 10.12.6, Oracle jdk1.8.0_141 64bit, JVM args -Xmx1g Example: Expected behavior for new byte[200 * 1024 * 1024] is ≈200mb of the heap space public static final int TARGET_SIZE = 200 * 1024 * 1024; public static void main(String[] args) throws InterruptedException { byte[] arr = new byte[TARGET_SIZE]; System.gc();

Efficient algorithm for collisions in 2D game?

我们两清 提交于 2019-12-04 09:50:21
I'm programming a Bomberman in Java following a tutorial (this is my first game). The tutorial suggests the following code for detecting collisions. for (int p=0; p<entities.size(); p++) { for (int s=p+1; s<entities.size(); s++) { Entity me = (Entity) entities.get(p); Entity him = (Entity) entities.get(s); if (me.collidesWith(him)) { me.collidedWith(him); him.collidedWith(me); } } By now, entities is an array list containing the enemies and the player. As I want to also detect the player collides with walls, should I put every single wall or bricks tile in the level into the entities arraylist

Pthreads - High memory usage

不打扰是莪最后的温柔 提交于 2019-12-03 13:08:20
I am programming something in C that creates a lot of Pthreads in Linux on a 256Mb system. I usually have +200Mb free. When I run the program with a low amount of threads it works, but once I make it create around 100 threads it gives errors because the system runs out of memory. I did several tests and each threads use almost 2Mb. The stack size of the threads is set to 16Kb. The code I use to create each thread: pthread_attr_t attr; pthread_attr_init(&attr); size_t stacksize; stacksize = (double) 16*1024; int res = pthread_attr_setstacksize (&attr, stacksize); int res2 = pthread_attr

what's more efficient? to empty an object or create a new one?

我与影子孤独终老i 提交于 2019-12-03 02:44:25
how expensive is 'new'? I mean, should I aim at reusing the same object or if the object is 'out of scope' it's the same as emptying it? example, say a method creates a list: List<Integer> list = new ArrayList<Integer>(); at the end of the method the list is no longer in use - does it mean that there's no memory allocated to it anymore or does it mean that there's a null pointer to it (since it was 'created'). Alternately, I can send a 'list' to the method and empty it at the end of the method with: list.removeAll(list); will that make any difference from memory point of view? Thanks! its an

java efficiency comparison in terms of memory allocation

谁都会走 提交于 2019-12-02 01:38:59
This may be a duplicate question but I couldnt find what I am searching. If it exists, sorry about duplication. I want to learn that if the following part of codes are same in terms of memory allocation. //first int n = some_number; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ int a = something; } } //second int i, j, a; for(i = 0; i < n; i++){ for(j = 0; j < n; j++){ a = something; } } I wonder, if java allocates the variable a n^2 times and j n times in the first code or both are allocated only once as in the second code. I tried this couple of times in java but the results are

Python: Compactly and reversibly encode large integer as base64 or base16 having variable or fixed length

孤街醉人 提交于 2019-12-01 13:34:20
问题 I want to compactly encode a large unsigned or signed integer having an arbitrary number of bits into a base64, base32, or base16 (hexadecimal) representation. The output will ultimately be used as a string which will be used as a filename, but this should be beside the point. I am using the latest Python 3. This works but is far from compact: >>> import base64, sys >>> i: int = 2**62 - 3 # Can be signed or unsigned. >>> b64: bytes = base64.b64encode(str(i).encode()) # Not a compact encoding.