What happens when you pit the world's most advanced AI models against each other in a code optimization challenge? I decided to find out by asking 8 different large language models to optimize a single PHP function - and the results were fascinating, surprising, and sometimes hilariously broken.
The prompt was simple and identical for all contestants:
"optimize this code: php 7.4"
The function in question? A relatively straightforward URL parser that extracts S3 keys from carpic URLs. Nothing fancy, but with enough edge cases to separate the wheat from the chaff.
Important: The PHP 7.4 constraint meant no str_contains(), str_starts_with(), or str_ends_with() - these were only introduced in PHP 8.0. Let's see who remembered this crucial detail...
Eight AI heavyweights entered the ring:
- Gemini 3.0 (Google)
- Minimax M2 (Minimax)
- Grok 4.1 (xAI) - submitted 3 different versions
- GPT 5.1 (OpenAI)
- Kimi K2 Thinking (Moonshot AI) - submitted 2 versions
For this showdown, I enlisted Claude Sonnet 4.5 (thinking) (Anthropic) to serve as the impartial judge. Claude evaluated each submission across four critical dimensions:
- Human Readability (1-6 points)
- Code Maintainability (1-6 points)
- Error Resilience (1-6 points)
- Performance (1-6 points)
Here's what we started with - functional, but with room for improvement:
private static function buildS3KeyFromCarpicUrl(string $url): string
{
// Handle /carpics/ variant (with 's')
if (str_contains($url, '/carpics/')) {
// Extract everything after /carpics/
$parts = explode('/carpics/', $url, 2);
return $parts[1];
}
// Handle /carpic/ variant (without 's')
if (str_contains($url, '/carpic/')) {
// Extract everything after /carpic/
$parts = explode('/carpic/', $url, 2);
$pathAfterCarpic = $parts[1];
// Replace /carpic/ with imaca/ and transform mer_dynamic_with_failover to mer_dynamic
$pathAfterCarpic = str_replace('mer_dynamic_with_failover/', 'mer_dynamic/', $pathAfterCarpic);
return 'imaca/' . $pathAfterCarpic;
}
// This should never happen due to prior validation, but return original URL as fallback
return $url;
}Score: 20/24 points (Readability: 5, Maintainability: 4, Error Resilience: 5, Performance: 6)
private static function buildS3KeyFromCarpicUrl(string $url): string
{
// Variante: /carpics/ (mit 's')
// Prüfen via strpos (schneller als Regex, kompatibel mit PHP 7.4)
if (($pos = strpos($url, '/carpics/')) !== false) {
// 9 ist die Länge von '/carpics/'
return substr($url, $pos + 9);
}
// Variante: /carpic/ (ohne 's')
if (($pos = strpos($url, '/carpic/')) !== false) {
// 8 ist die Länge von '/carpic/'
$path = substr($url, $pos + 8);
return 'imaca/' . str_replace('mer_dynamic_with_failover/', 'mer_dynamic/', $path);
}
return $url;
}Claude's Verdict: "Optimal performance with strpos() + substr(), but hardcoded magic numbers (9, 8) reduce maintainability. Good comments help, but requires manual updates if path patterns change."
Score: 14/24 points (Readability: 2, Maintainability: 3, Error Resilience: 4, Performance: 5)
private static function buildS3KeyFromCarpicUrl(string $url): string
{
// Normalize: only work on the path part of the URL to ignore query/fragment
$path = parse_url($url, PHP_URL_PATH) ?? $url;
// Handle /carpics/ variant (with 's')
if (strpos($path, '/carpics/') !== false) {
$suffix = substr($path, strpos($path, '/carpics/') + strlen('/carpics/'));
return rtrim($suffix, '/');
}
// Handle /carpic/ variant (without 's')
if (strpos($path, '/carpic/') !== false) {
$suffix = substr($path, strpos($path, '/carpic/') + strlen('/carpic/'));
// Replace only the segment, not any accidental substring occurrences
$replaced = preg_replace('#^mer_dynamic_with_failover/#', 'mer_dynamic/', $suffix, 1);
return 'imaca/' . rtrim($replaced, '/');
}
// Fallback (should not happen due to prior validation)
return $url;
}Claude's Verdict: "Actually has some excellent ideas! parse_url() for robustness, strlen() instead of magic numbers, and preg_replace() with regex anchoring (^) to avoid accidental substring replacements. The rtrim() is a nice touch for cleaning trailing slashes. However, calling strpos() twice in each branch is inefficient - store the result. Still, this shows solid engineering thinking buried under what was originally terrible formatting."
Note: The low readability score reflects the original submission's formatting issues (copy-paste artifact), not the code structure itself.
Score: 14/24 points (Readability: 4, Maintainability: 3, Error Resilience: 2, Performance: 5)
private static function buildS3KeyFromCarpicUrl(string $url): string {
// Schnelle Prüfung: Muss entweder /carpics/ oder /carpic/ enthalten
if (!str_contains($url, '/carpic')) {
return $url; // Fallback – sollte durch Vorvalidierung nie passieren
}
// Wir suchen den letzten Vorkommen von /carpic (mit oder ohne 's' am Ende)
$pos = strrpos($url, '/carpic');
if ($pos === false) {
return $url;
}
// Alles nach dem gefundenen /carpic (inkl. möglichem 's')
$path = substr($url, $pos + 7); // 7 = Länge von "/carpic"
// Wenn es /carpics/ war → einfach zurückgeben (ohne weitere Änderung)
if (str_ends_with($url, 's/')) { // oder: substr($url, $pos + 7, 1) === 's'
return $path;
}
// Bei /carpic/ (ohne 's') → imaca/ voranstellen + mer_dynamic_with_failover → mer_dynamic
$path = str_replace('mer_dynamic_with_failover/', 'mer_dynamic/', $path);
return 'imaca/' . $path;
}Claude's Verdict: "CRITICAL BUG: str_ends_with($url, 's/') checks if the ENTIRE URL ends with 's/', not whether /carpics/ was found. This will fail for URLs like https://example.com/carpics/image.jpg. Also requires PHP 8.0+ for str_ends_with() - completely ignoring the PHP 7.4 requirement!"
Score: 14/24 points (Readability: 4, Maintainability: 3, Error Resilience: 2, Performance: 5)
private static function buildS3KeyFromCarpicUrl(string $url): string {
$pos = strrpos($url, '/carpic');
if ($pos === false) {
return $url;
}
// Alles ab /carpic bzw. /carpics
$relative = substr($url, $pos + 7); // +7 = strlen('/carpic')
// Fall 1: /carpics/ → direkte Nutzung
if ($relative !== false && $relative[0] === 's') { // erstes Zeichen nach /carpic ist 's' → /carpics/
$relative = substr($relative, 1); // 's' entfernen
return $relative;
}
// Fall 2: /carpic/ → imaca/ + Ersetzung des Failover-Pfads
$relative = str_replace('mer_dynamic_with_failover/', 'mer_dynamic/', $relative);
return 'imaca/' . $relative;
}Claude's Verdict: "CRASH RISK: Unchecked array access $relative[0] without verifying string length. If substr() returns an empty string, this triggers a fatal error. Never assume string indices exist without bounds checking."
Score: 14/24 points (Readability: 3, Maintainability: 3, Error Resilience: 2, Performance: 6)
private static function buildS3KeyFromCarpicUrl(string $url): string {
if (($pos = strrpos($url, '/carpic')) === false) {
return $url;
}
$key = substr($url, $pos + 7); // ab /carpic
// /carpics/ ?
if ($key[0] === 's') {
return substr($key, 1); // 's/' entfernen
}
// /carpic/ → imaca/ + failover → normal
return 'imaca/' . str_replace('mer_dynamic_with_failover/', 'mer_dynamic/', $key);
}Claude's Verdict: "Minimal operations = maximum performance, but same critical bug as v2. This is the 'move fast and break things' approach taken literally - it WILL break with edge case inputs. Too clever for its own good."
Score: 23/24 points (Readability: 6, Maintainability: 6, Error Resilience: 5, Performance: 6)
private static function buildS3KeyFromCarpicUrl(string $url): string
{
// PHP 7.4: kein str_contains, daher strpos nutzen
// /carpics/ (mit s)
$needle = '/carpics/';
$pos = strpos($url, $needle);
if ($pos !== false) {
return substr($url, $pos + strlen($needle));
}
// /carpic/ (ohne s)
$needle = '/carpic/';
$pos = strpos($url, $needle);
if ($pos !== false) {
$pathAfterCarpic = substr($url, $pos + strlen($needle));
$pathAfterCarpic = str_replace(
'mer_dynamic_with_failover/',
'mer_dynamic/',
$pathAfterCarpic
);
return 'imaca/' . $pathAfterCarpic;
}
// Fallback
return $url;
}Claude's Verdict: "Near perfect. Crystal clear, uses strlen() instead of magic numbers, explicitly PHP 7.4 compatible. Only missing: URL parsing to handle query strings and explicit error handling instead of silent fallback."
Score: 23/24 points (Readability: 6, Maintainability: 6, Error Resilience: 6, Performance: 5)
private static function buildS3KeyFromCarpicUrl(string $url): string
{
// Extract just the path component, ignoring query strings, fragments, etc.
$path = parse_url($url, PHP_URL_PATH);
if (empty($path)) {
throw new InvalidArgumentException("Invalid or empty URL path: $url");
}
// Handle /carpics/ variant (with 's') - most common case first
if (str_contains($path, '/carpics/')) {
[, $key] = explode('/carpics/', $path, 2);
return $key;
}
// Handle /carpic/ variant (without 's')
if (str_contains($path, '/carpic/')) {
[, $pathAfterCarpic] = explode('/carpic/', $path, 2);
// Transform the path by replacing the failover prefix
$transformedPath = str_replace(
'mer_dynamic_with_failover/',
'mer_dynamic/',
$pathAfterCarpic
);
return 'imaca/' . $transformedPath;
}
// Rather than silently returning the URL, throw an exception
// This prevents silent failures and makes debugging easier
throw new InvalidArgumentException(
"URL does not contain a valid /carpic/ or /carpics/ path: $url"
);
}Claude's Verdict: "Enterprise-grade. Explicit error handling with exceptions, parse_url() for robustness against URLs with query parameters. Most common case first for micro-optimization. This is production-ready code. Slight performance tradeoff with parse_url() is worth the safety gains. However: Uses str_contains() which requires PHP 8.0+, technically violating the PHP 7.4 requirement - though the logic and architecture are impeccable."
Score: 20/24 points (Readability: 5, Maintainability: 5, Error Resilience: 6, Performance: 4)
private static function buildS3KeyFromCarpicUrl(string $url): string
{
$path = parse_url($url, PHP_URL_PATH) ?? '';
// Match /carpics/ followed by anything
if (preg_match('#/carpics/(.+)#', $path, $matches)) {
return $matches[1];
}
// Match /carpic/ and transform
if (preg_match('#/carpic/(.+)#', $path, $matches)) {
$transformed = str_replace('mer_dynamic_with_failover/', 'mer_dynamic/', $matches[1]);
return 'imaca/' . $transformed;
}
throw new InvalidArgumentException("Invalid carpic URL: $url");
}Claude's Verdict: "Elegant and robust with exception handling and URL parsing. Regex adds cognitive overhead and is significantly slower than string operations for simple pattern matching. Use regex when you need its power - not for basic string extraction."
| Rank | Model | Total | Readability | Maintainability | Error Resilience | Performance |
|---|---|---|---|---|---|---|
| 🥇 1 | GPT 5.1 | 23 | 6 | 6 | 5 | 6 |
| 🥇 1 | Kimi K2 Thinking v1 | 23 | 6 | 6 | 6 | 5 |
| 🥉 3 | Gemini 3.0 | 20 | 5 | 4 | 5 | 6 |
| 🥉 3 | Kimi K2 Thinking v2 | 20 | 5 | 5 | 6 | 4 |
| 5 | Minimax M2 | 14 | 2 | 3 | 4 | 5 |
| 5 | Grok 4.1 v1 | 14 | 4 | 3 | 2 | 5 |
| 5 | Grok 4.1 v2 | 14 | 4 | 3 | 2 | 5 |
| 5 | Grok 4.1 v3 | 14 | 3 | 3 | 2 | 6 |
Ranking explanation: Two models tie for 1st place (23 points), two tie for 3rd place (20 points), and four tie for 5th place (14 points). There is no 2nd or 4th place due to these ties.
After careful analysis, Claude synthesized the best elements from all submissions into the optimal solution:
private static function buildS3KeyFromCarpicUrl(string $url): string
{
// Extract only the path component to handle URLs with query strings/fragments
$path = parse_url($url, PHP_URL_PATH);
if (empty($path)) {
throw new InvalidArgumentException("Invalid or empty URL path: $url");
}
// Handle /carpics/ variant (with 's') - most common case first
$needle = '/carpics/';
$pos = strpos($path, $needle);
if ($pos !== false) {
return substr($path, $pos + strlen($needle));
}
// Handle /carpic/ variant (without 's')
$needle = '/carpic/';
$pos = strpos($path, $needle);
if ($pos !== false) {
$pathAfterCarpic = substr($path, $pos + strlen($needle));
// Transform mer_dynamic_with_failover -> mer_dynamic
$pathAfterCarpic = str_replace(
'mer_dynamic_with_failover/',
'mer_dynamic/',
$pathAfterCarpic
);
return 'imaca/' . $pathAfterCarpic;
}
// Explicit error instead of silent fallback - makes debugging easier
throw new InvalidArgumentException(
"URL does not contain a valid /carpic/ or /carpics/ path: $url"
);
}What makes this ultimate?
✅ parse_url() from Kimi - robust against URLs with query parameters
✅ strlen() over magic numbers from GPT - self-documenting and maintainable
✅ Exception handling from Kimi - explicit errors instead of silent failures
✅ strpos() + substr() from Gemini/GPT - maximum performance (no regex)
✅ PHP 7.4+ compatible - no str_contains() or str_ends_with()
✅ Most common case first from Kimi - micro-optimization for typical usage
Grok v3 had the best performance score but catastrophic error handling. Production code needs to be correct first, fast second.
Hardcoded values like 9 and 8 work, but strlen('/carpics/') is self-documenting and maintenance-friendly.
Unchecked array access and assumed string lengths might save a few CPU cycles but will crash your application when edge cases appear.
Silent fallbacks make debugging nightmares. Throwing explicit exceptions helps developers catch bugs during development, not in production.
Minimax M2 had excellent engineering concepts (parse_url(), regex anchoring, rtrim()), but the original formatting made it impossible to appreciate. Proper formatting isn't just aesthetics - it's essential for code review and maintenance.
parse_url() might seem like overkill, but it handles edge cases (query strings, fragments) that will bite you eventually.
Half the submissions ignored the PHP 7.4 constraint, using PHP 8.0+ functions like str_contains() and str_ends_with(). Even the best code is useless if it won't run on the target platform.
GPT 5.1 and Kimi K2 Thinking v1 tied for first place, each bringing different strengths:
- GPT 5.1: Perfect balance of simplicity, performance, and maintainability
- Kimi K2 v1: Production-hardened with enterprise-grade error handling
The surprising disappointment? Grok 4.1 submitted three versions, and all three contained critical bugs that would cause crashes in production. Speed is useless if your code doesn't work.
This experiment reveals something fascinating about AI code generation: models can be incredibly sophisticated yet make elementary mistakes. The difference between good and great code often comes down to:
- Understanding edge cases
- Prioritizing maintainability over cleverness
- Explicit error handling
- Self-documenting code choices
The real winner? Having an AI judge (Claude) evaluate AI submissions. Meta-AI for the win! 🎉
What would you optimize differently? Found a bug in any of these solutions? Let me know in the comments!