You are so unlucky.
For the first time in his life, YDJSIR encountered the difficulties of cross-platform development.
It is well known that JAVA is a cross-platform language, various JVMs enable it to run smoothly on a billion devices, but is its cross-platform nature impeccable?
Due to my lack of knowledge, I can only describe the phenomenon rather than analyze the specific reasons. YDJSIR would be very grateful if someone could help explain the reasons.
Here is an example.
**References: **https://github.com/XZ-X/2020SE1-FAQ/issues/13
Problem Description
Expected Result
AC
Actual Result
Pass locally, TLE on the OJ.
Code Analysis
Original Code
1 | //Determine whether the given string is a legal directory relative path in Linux: whether it is a set of/concatenated strings consisting only of numbers, letters, and underscores (ideally, of course) |
There seems to be no problem. At this point, YDJSIR also takes a good dig at YDJSIR’s roommate to use case-oriented programming.
Test Results
Local Tests-Windows 10 x64
Windows10 Version
LocalJDKVersion
1 | openjdk version "1.8.0_242" |
IDEA Tests Results
IDEA-Tests-(Junit4)-Results. Fine. 94ms.
mvn test
Results
1 | Tests run: 9, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.133 sec |
本处省略大量e.printbacktrace()
的内容.
1 | Results : |
Here are tests from CZG.
Linux AdoptOpenJDK 1.8 TestsResults
118ms, Fine(Linux - Arch)
Oracle JDK8 TestsResults
JDKVersion
1 | jdk1.8.0_251 |
IDEA TestsResults
There seems to be something wrong…
mvn test
TestsResults
1 | ------------------------------------------------------- |
Fine. YDJSIR is going to push my code.
OJ-CentOS x64 TestsResults
CentOS
JDKVersion
1 | jdk-8u161-linux-x64 |
OJ-mvn test
Results
1 | ------------------------------------------------------- |
According to the explanation of the TA, the server gives all test cases 1 minute at most. YDJSIR’ i5-9300 - H
, is powerful, and so as the performance of the server. More than one order of magnitude between 94 ms and 1 min is not reasonable. This error is not a compile error, which adds more difficulties to the problem. If it is a compile error, then YDJSIR finds out where the error is. However, it is not.
Therefore, YDJSIR proposed an issue. TA thought it was the problem of the Maven package at first, and let YDJSIR run MVN test.
YDJSIR at first lacked the package due to network problems, but after the package was installed, my code just doesn’t work on the OJ.
After that, the feedback from the platform teaching assistant was that the system was not deployed/built successfully, so it was impossible to judge which tests failed.
At the same time, there are two ultra-realistic statements.
There is someone who AC, so we can’t modify the OJ for you alone, yes?
If the problem cannot be solved, please let the teaching assistants run your code locally and give you scores.
Then YDJSIR was trapped into the loop of DEBUG.
MAC- TestsResults
MAC
Local JDK
1 | jdk-8u251-macosx-x64 |
IDEA TestsResults
MVN TestsResults
More than 20s. The test is interrupted.
Solutions
Basic: No Regular Expressions
The code here comes from YDJSIR’s roommate.
1 | String substring = classpath.substring(classpath.length() - 4, classpath.length()); |
LocalIDEA Results
AC should go first!
OJ Results
1 | T E S T S |
Advanced: Improve Regular Expression
After my AC, YDJSIR went back and read the Zhihu column again and again (the link at the beginning) and seemed to find something. Thus, the regular expression of YDJSIR became as follows.
1 | String patternDir = "^(\\w+\\/?)+$"; |
LocalIDEA Results
Fine. 94ms
OJ Results
AC.
1 | T E S T S |
What’s the problem?
. | Matching any single char except ‘\n’. To match ‘.’,use ‘\. ‘ please. |
---|---|
https://www.runoob.com/regexp/regexp-syntax.html |
This gap in time… Well, it’s still scary.
Results
Summary
Environment | Original | Modified |
---|---|---|
YDJSIR-IDEA-OpenJDK 1.8.0_242-b08 Win_x64 | 94ms | 58ms |
YDJSIR-MVN-OpenJDK 1.8.0_242-b08 Win_x64 | 133ms | 201ms |
YDJSIR-IDEA-OracleJDK jdk-1.8.0_251 Win_x64 | 81sec | 14sec |
YDJSIR-MVN-OracleJDK jdk-1.8.0_251 Win_x64 | 81sec | 15sec |
YDJSIR -IDEA-OracleJDK jdk-14.0.1 Win_x64 | 115ms | 106ms |
YDJSIR -IDEA-OracleJDK jdk-14.0.1 Win_x64 | 123ms | 73ms |
MAC-IDEA-OracleJDK jdk-1.8.0_251 Mac_x64 | ==TLE== | 17sec |
CZG VM-MVN-OpenJDK 1.8.0_242-b08 Linux_x64(Arch) | 118ms | ==Not Tested== |
OJ ECS-MVN-OracleJDK jdk-1.8.0_251 Linux_x64(CentOS 7.7) | ==TLE== | 16s |
CX MAC-IDEA-OracleJDK jdk-11 Mac_x64 | 90ms | 40ms |
CX MAC-MVN-OracleJDK jdk-11 Mac_x64 | 180ms | 107ms |
The Time limit here is 1min in one thread.
- Different JDKs have different implementation for the same syntax, so the application of language features should be extremely cautious. Even for JDK 1.8, its implementation between OpenJDK and that of Oracle JDK is probably not the same. From 58 ms to 16 seconds, nearly two orders of magnitude of the gap cause great confusion. However, this problem is challenging to be found unless the production environment and the actual environment are of the same configuration.
- Small mistakes, not a Critical error, may lead to considerable confusion. There is no escape. The brought from 58 ms to 94 ms changes, and in OracleJDK, the brought from 16 s to 81 s, the dramatic differences between the two can be amazing. However, the former takes a difference close to 2.8 times, while the latter nearly five times.
- It makes sense to adopt newer technologies as much as possible in development. JAVA 11 is also an LTSVersion, and although the older version may have more resources and less unknown bugs, the improvements made by the new technology from the bottom should be recognized.
From Wiki
Postscripts
YDJSIR finds that IDEA may at first using its own JDK to run Tests, rather than an MVN test.
The YDJSIR thinks this statement is reasonable. YDJSIR IDEA Local Tests initially passed even before installing the pack needed by MVN. The characteristic feature of this phenomenon is that the SDK Settings have never been changed by default. However, the results obtained by YDJSIR in the official IDEA document is that the JDK is only used for the operation of the IDE itself, not for development, and you need to set the JDK by yourself.
References: https://www.jetbrains.com/help/idea/sdk.html#
1 | Important notice |
YDJSIR doesn’t know how to explain these words.
References: https://intellij-support.jetbrains.com/hc/en-us/community/posts/360006922240-Do-I-have-to-install-JDK-
The experiment proved that IDEA would compile and run the program with its own JDK(based on the OpenJDK, called JBR) by default. After setting up its JDK, YDJSIR did not find a way to use only the native JDK. To reduce the situation, YDJSIR selects the Oracle JDK to complete the YDJSIR’s job (DDL)
1 | JetBrains Runtime is a runtime environment for running IntelliJ Platform-based products on Windows, Mac OS X, and Linux. JetBrains Runtime is based on the OpenJDK project with some modifications. These modifications include Subpixel Anti-Aliasing, enhanced font rendering on Linux, HiDPI support, ligatures, some fixes for native crashes not presented in official builds, and other small enhancements. |
From the website of JBR.
YDJSIR is too inexperienced to address these issues. To aid in this, here is the information about the Local IDEA Version.
Conclusions
After a series of twists and turns, YDJSIR came to the following conclusion:
In programming, any character that can be typed directly from a keyboard should be treated with caution! Even a Numpad might have 114,514 strange functions!
Regular expressions have many holes that you can fall into if you’re not careful enough.
Case oriented programming is sometimes a proper technique. However, YDJSIR is willing to play around when possible;
It is dangerous not to learn something well. For example, the missing escape character YDJSIR leads to a timeout on the server, which can be disastrous in a production environment.
YDJSIR has been learning to code so long that the feeling of programming ability has not improved, but the DEBUG experience does increase indeed. YDJSIR will abandon the way of printing all and use more IDE’s features to DEBUG.