Hello, %username%!
Suppose we have statement and tests of some task.
We want to check our solution with given time && memory limit.
We can do this using .bat — script with run.exe.
But how to do this on linux without using wine?
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
Hello, %username%!
Suppose we have statement and tests of some task.
We want to check our solution with given time && memory limit.
We can do this using .bat — script with run.exe.
But how to do this on linux without using wine?
Name |
---|
A manual way:
Type in
bash
to create a new shell process.In the new shell, run
ulimit -v %your_memory_limit_in_KB%
to set a memory limit. The limit will be applied to all child processes of the shell.Then simply use
time ./a.out <input >output
to run your programa.out
. The running time of your program will be printed tostderr
and you can see it on screen.After that type
exit
to quit the new shell and cancel the limit of memory.To make the process automatically, you can write a bash script just like bat in Windows.
I've also seen ulimit -m, but couldn't figure out how does it work. Does it even work?
ulimit -m
sets a limit to physical memory. That means if this limit is exceeded, system will put some data intoswap
partition. Useulimit -v
instead. It sets a limit to virtual memory. It works as I tested.Oh I see now, OJs count virtual memory use, I've tested now. I thought they count physical memory use. Yes, it works, thanks.
OFFTOP Question: Can we measure time limit and memory limit by python script. (If yes how?).
Yes: https://docs.python.org/3/library/resource.html
May we use result of function time as variable?
Yes, but you should change time's output format (in order to print the value by itself). For example:
(where 3 is the decimal precision)
This will execute a command (for example:
/bin/echo
but you can change it to./program
or something) and redirect its stdout to /dev/null (because all we want is stderr, where time will print the time).To save that value in a variable, you will do:
If I understand correctly, "ulimit -v" limits the size of address space, not the actual memory usage. In such case it is totally incorrect; it may be close to actual memory usage for trivial C/C++ programs, but that's pretty much the only case when it is close to memory usage. Non-trivial C/C++ programs, or programs running in a special environment (i.e. Java, Python, ... programs) will have it much larger than the actual memory usage.
But it looks that OJs (at least codeforces) count virtual memory: 9476756
How very "nice" of them. This is pretty evil: the actual amount of memory "used" for complex programs is going to be huge this way. Also, if there is a task with large input, and someone decides to mmap() it, he'll get an unpleasant surprise.
I wonder why don't they just use resident size of the process. This should be the correct value.
Besides, other online judges (e.g., ejudge and Yandex.Contest) use the right value. Maybe it is only a problem of windows-based invokers?
When someone mmap()s a file, he is effectively using kernel memory to hold portions of the file. Is it fair not to count it towards memory usage?
It is fair to count these portions, but unfair to count the entire file. Note that regardless of mmap, everyone who reads something from disk, (indirectly) uses kernel buffers. If organizers are afraid of the abuse, they can just limit total size of kernel buffers by something small.
My response is wrong. In fact the reason of failure of
ulimit -m
is following in the man page ofbash
:-m The maximum resident set size ( many systems do not honor this limit )
Anyone can see it by execute
man bash
then type/ulimit
. So unfortunately we can only use the limit to virtual memory. Maybe someone can modify Linux kernel to control resident set size.There is no need to modify Linux kernel: such limits can easily be enforced via cgroups.
Oh... Pity I didn't know it. Thanks.
Only to set the time limit timelimit.
It's better to watch the execution time using
time ./a.out < in > out
. While the judging system only cares about the binary can pass / can't pass, you care about how fast it is to estimate its chances.Chortos-2 has written a tool in Python to help the testing of solutions locally. You can check it out here.
How about other operating systems like windows? How can I check memory limit in my C++ codes?
There is programm "run" which was written by andrewzta link