Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C# lost to PHP, and I lost my Job

Name: Anonymous 2014-02-08 16:12

Hey! It is me again.

My last posting here was about breakage with GF and that a new .NET job would be much cooler than the former C/C++ one. So the first task, employer gave me, was rewriting legacy PHP integration layer into C#.

The assignment sounded easy, because C# is a compiled language, having limited type inference. Alas after writing the initial prototype, I found that C# fails to give me a way to do efficiently something as a simple text data storage. Yes, after all the rewriting and optimization, PHP version was still faster and taking less memory than C# one, which continuously spitted System.OutOfMemoryException. Other programmers at the workplace were enraged, that I lost C#'s honor to some PHP. But the reality is that C# loses to PHP, when it comes to memory usage.

The PHP code consited of a sender.php, which repeatedly gets called by a cron job to spawn new processes, and a lock.file, which these processes lock before accessing DB. Another watchdog cron job moderates the number of these treads, killing unwanted, so that they won't eat all resources. The system can sustain around 30 of these PHP processes at any single moment. Each sender.php process fetches 10 mb of UTF8 messages from an SQL DB, converts them into x-www-form-urlencoded, compresses through libbz2 and sends the resulting stream as a POST http query to the destination. After transfer is complete, PHP gets killed, freeing all the resources.

In my C# version I copied PHP's behavior exactly: 30 threads taking messages in parallel and establishing 30 connections to destination. Yet C# version was taking 100 megabytes per thread, while PHP only 20mb. The problem unfolded as following:
1. Just like PHP, each C# thread fetched 10 mb of UTF8 messages
2. Yet unlike PHP, C# immediately converts fetched UTG8 bytes into UTF16 (string[]'s internal representation), allocating another 20mb and giving total of 30mb
3. urlencode further converts them, in some cases tripling the size due to the %DE%AD look; adding 40mb, due to the need for StringBuilder to generate the PHP's &hash_map[key0][key1]=fuck+you arrays.
4. Here inefficient bzip2 implementation kicks-in, taking 6 seconds to compress 10mb. Note that PHP's libbz2 is written in C/C++.
5. All C#'s threads are running in a single process, so nothing is freed, after o single thread finishes. This really stresses memory, compared to PHP's shared nothing architecture. And NO, garbage collector doesn't free memory, until it is too late and System.OutOfMemoryException is generated. Even explicitly forcing GC wont help.

All in one, I lost to PHP and lost my job, because team lead got really aggravated to my failure and that I had nerve to defend PHP's approach to memory management. I'm in search of a new job, my dear /prog/. Hope this time it will be at least Clojure, because it uses UTF8 to store strings and Java's GC doesn't produce System.OutOfMemoryException out of the blue.

http://www.youtube.com/watch?v=oxL_xY0Tm2w

Name: Anonymous 2014-02-08 16:30

Yes, after all the rewriting and optimization, PHP version was still faster and taking less memory than C# one
I thought C# was infinitely better than ElePHPant shit?

Name: Anonymous 2014-02-08 16:37

>>2
I thought C# was infinitely better than ElePHPant shit?
1. PHP doesn't leak memory: http://en.wikipedia.org/wiki/Shared_nothing_architecture
2. PHP's routines (like urlencode and libbz2) are written in C/C++

Name: Anonymous 2014-02-08 19:10

Why were you getting out of memory errors if you say the C# SOLUTION took only 100MB of RAM?

Sure, 5x more than PHP, but still.

Name: Anonymous 2014-02-08 19:53

>>4

Why were you getting out of memory errors if you say the C# SOLUTION took only 100MB of RAM?
each of 30 threads took 100MB, resulting into 3GB.

Also, incorrect memory pressure declarations on behalf of some unmanaged code could too lead to such problem. I.e. forgetting to call GC.AddMemoryPressure leads to memory leak and System.OutOfMemoryException, unless you force garbage collection.

Name: Anonymous 2014-02-08 19:53

I'm sorry to hear you lost your job great wizard Nikita.

Good luck in the future!

Name: Anonymous 2014-02-08 21:56

http://systematicgaming.wordpress.com/2009/01/03/performance-c-vs-c/
In case you can’t read that properly, the C++ version is 10 times faster than the C# version. You can also see that the lines are nice and parallel, so that’s no algorithmic difference in sorting routines, both implementations have the same runtime complexity. We’re seeing 10 times overhead in execution.

That is why they don't use C# for AAA video game titles.

Name: Anonymous 2014-02-09 1:56

>>7
Some butt ravaged C# programmer wrote a faster implementation in C#.

http://journal.stuffwithstuff.com/2009/01/03/debunking-c-vs-c-performance/

If I had the patience to participate in shit like this, I would write an equivalent C++ program to the one ey posted and get the 10x speed increase again, but alas, I don't give a shit.

Name: Anonymous 2014-02-09 2:02

I bet an experienced C# programmer without any C++ experience can write code that proves exactly the opposite by writing bad C++ code :)

Basicly he would do to the C++ code what you did to the C# code : try to port it directly without an understanding of the language and the way it’s supposed to be used.
Which means he would first write a garbagecollector in C++ etc, and then shout it wouldn’t perform up to par with C# code…. :)

I want to strangle this person.

Name: Anonymous 2014-02-09 6:39

I am praying for you Nikita. I hope you find solace.

Name: Anonymous 2014-02-12 18:07

Nikita: if given the choice between a PHP job and a C#, which one would you pick, considering what just happened?

I'm asking you this because you're a Lisp wizard and I'd like to know if you'd really prefer a language made by Jews over one which has lambdas and LINQ.

Name: Anonymous 2014-02-12 20:30

>>11
given the choice between a PHP job and a C#, which one would you pick
nodejs

Name: Anonymous 2014-02-12 22:33

>>12
Oh, come on. You don't have to get drastic over it. If you pick PHP, for instance, you could do all your work in some other language, then just call bindings to it. You'd have to face burning shame every day, but at least you could preserve a modicum of honor.

Name: Anonymous 2014-02-12 22:56

>>12
Did we get a special visit from the Javashit kike?

Name: Anonymous 2014-02-13 3:29

>>13
You can compile some other language with asmjs.

Name: Anonymous 2016-06-23 15:24

Check em

Name: Anonymous 2016-06-23 22:00

Third hypothesis: You're a shitty programmer.

Name: Anonymous 2016-06-24 14:22

>>17
You're underestimating PHP, that was developed for memory-limited machines, so PHP's internal data structures could take less memory. I.e. by using utf8 strings, instead of utf16.

Name: Anonymous 2016-06-24 14:33

If it ain't LISP it's shit.

Name: Anonymous 2016-06-24 15:28

Check em

Name: Anonymous 2016-06-25 13:50

>>1
is written in C/C++
What programming language is this?

Name: Anonymous 2016-06-25 14:10

Uh, maybe invoke bzip2 as a shell command rather than using the native C# implementation.

If GC can't keep up with your program, you're probably leaking memory.. fix that, or rewrite it to spawn separate processes rather than threads. More overhead but they should survive.

Name: Anonymous 2016-06-25 14:12

>>21
C/C = 1 for any values of C (except zero).
so C/C++ is 2.

Name: Anonymous 2016-06-25 15:03

>>22
Back then I was disallowed by my employer from using any external utilities. There was tight review process and unit testing. So using external dependency added whole layer of complexity, than using shoddily made C# bzip2

Name: Anonymous 2016-06-25 15:08

>>22
If GC can't keep up with your program, you're probably leaking memory.. fix that,
Nope. I had no leaks. The code was simple enough to review it for leakages. Read OP post carefully. That happened because of several threads competing for memory.

The only reliable way to free memory with C# is to start separate process and kill it, after it finishes the work, but that would be too slow for many applications, because of runtime initialization times.

Name: Anonymous 2016-06-25 15:11

>>23
No, ++C/C would be 2. C/C++ is 1 and then an increment.

Name: Anonymous 2016-06-25 17:09

>>26
C = -2
C/C++ = 1

Name: Anonymous 2016-06-26 16:17

>>27
Yeah, that's what I said, C/C++ is 1. Even if C = -2. Your point being?

Don't change these.
Name: Email:
Entire Thread Thread List